Python “最后一次迭代生成”;无”;输出

Python “最后一次迭代生成”;无”;输出,python,pexpect,Python,Pexpect,我正在编写一个脚本,使用Pexpect和regex测试输出交换机和公共IP之间的延迟 这里有一个示例: # Connect to a cisco system just before and going enable for key in nodes: ipaddr_node = nodes[key]["IP Address"] print ('[|] Ping de %s en cours ...' % ipaddr_node) p.sendline("ping %s

我正在编写一个脚本,使用
Pexpect
和regex测试输出交换机和公共IP之间的延迟

这里有一个示例:

# Connect to a cisco system just before and going enable
for key in nodes:
    ipaddr_node = nodes[key]["IP Address"]
    print ('[|] Ping de %s en cours ...' % ipaddr_node)
    p.sendline("ping %s repeat 20" % ipaddr_node) #ping of the ip 20 times on cisco
    p.expect('#')
    ping = p.before #get the output before '#'
    print ('[+] Ping de %s reussi' % ipaddr_node)
    place = ping.find('min') #get the position of 'min' in output
    regex = ping.replace(ping[:place],"")
    output = re.search(r'\s=\s(?P<min>\d{1,4}.\d{0,3})\/(?P<avg>\d{1,4}.\d{0,3})\/(?P<max>\d{1,4}.\d{0,3})', regex) #regex to get min, avg and max
    print(output) #Print regex object
    avg = output.group('avg') #get value of group "avg" in regex
    print('[+] Average time : ' + avg) #print it
#在启用前连接到cisco系统
对于键入节点:
ipaddr_node=节点[键][“IP地址”]
打印('[|]在进程中对%s进行Ping…'%ipaddr\u节点)
p、 发送线(“ping%s重复20”%ipaddr_节点)#在cisco上ping ip 20次
p、 期望(“#”)
ping=p.before#在“#”之前获取输出
打印('[+]Ping de%s reussi'%ipaddr\u节点)
place=ping.find('min')#获取输出中'min'的位置
regex=ping.replace(ping[:place],“”)
output=re.search(r'\s=\s(?P\d{1,4}.\d{0,3})\/(?P\d{1,4}.\d{0,3})\/(?P\d{1,4}.\d{0,3}),regex)获取min、avg和max
打印(输出)#打印正则表达式对象
avg=output.group('avg')#获取regex中组“avg”的值
打印('[+]平均时间:'+avg)#打印
下面是一个输出示例:

# Connect to a cisco system just before and going enable
for key in nodes:
    ipaddr_node = nodes[key]["IP Address"]
    print ('[|] Ping de %s en cours ...' % ipaddr_node)
    p.sendline("ping %s repeat 20" % ipaddr_node) #ping of the ip 20 times on cisco
    p.expect('#')
    ping = p.before #get the output before '#'
    print ('[+] Ping de %s reussi' % ipaddr_node)
    place = ping.find('min') #get the position of 'min' in output
    regex = ping.replace(ping[:place],"")
    output = re.search(r'\s=\s(?P<min>\d{1,4}.\d{0,3})\/(?P<avg>\d{1,4}.\d{0,3})\/(?P<max>\d{1,4}.\d{0,3})', regex) #regex to get min, avg and max
    print(output) #Print regex object
    avg = output.group('avg') #get value of group "avg" in regex
    print('[+] Average time : ' + avg) #print it

('min/avg/max=33/44/51 ms\r\nRTR LAB GRE','找到解决方案!

这是我在正则表达式搜索中的一个简单错误。 这是旧的:

这是新的:

为了简化,我的第一个请求找不到最后一行,因为我的
没有正确转义。 我只是在两种可能性之间添加了一个很好的转义加上一个


感谢您的帮助。

找到了解决方案!

这是我在正则表达式搜索中的一个简单错误。 这是旧的:

这是新的:

为了简化,我的第一个请求找不到最后一行,因为我的
没有正确转义。 我只是在两种可能性之间添加了一个很好的转义加上一个


谢谢您的帮助。

输出似乎与代码不对应。代码中的
打印
消息不同。您打算用模式
\d{1,4}.\d{0,3}表示什么
?您能用自己的话解释一下这意味着什么吗?这是一个简单的正则表达式,用于从ping命令获取输出时间。是的,打印代码不同,但这只是显示,代码保持不变。输出似乎与代码不符。代码中的
打印
消息不同。您有什么看法倾向于通过模式
\d{1,4}.\d{0,3}
来表达?你能用自己的话解释一下这意味着什么吗?这是一个简单的正则表达式,可以从ping命令中获得输出时间。是的,打印代码不同,但这只是显示,代码保持不变