我希望读取(重复)输出命令的每一行,如果条件匹配,则打印行,如果不匹配,则保持重复循环直到超时,并打印当前输出或一条消息
它在打印行或超时两种情况下都保持循环
我只想打印一次行,或者在超时的情况下超时一次,并且在max_time_allowed中继续尝试前面的命令,因为它可能会在几秒钟后出现
#Import ConncetHandler feature from netmiko
from netmiko import ConnectHandler
import time
import re
import sys
import subprocess
import os
#Devices Details
iosv_l2_s1 = {
'device_type': 'cisco_ios',
'ip': '1.1.1.1',
'username': 'sdf',
'password': 'ss',
}
#SSH to the below devices
net_connect = ConnectHandler(**iosv_l2_s1)
#Apply commands
output = net_connect.send_command('show interface')
max_time_allowed = 15
start = time.time()
while True:
for line in output.split(os.linesep):
if re.search(("(?=.*FUlL|2way)(?=.*192.168.0.4)"),line , re.I):
print line
start = time.time()
time.sleep(2)
elif (time.time() - start) > max_time_allowed:
print ("timeup")
net_connect.disconnect()发布于 2017-07-30 12:35:02
将while循环更改为在到达时间或执行print语句时停止
max_time_allowed = 15
start = time.time()
found = 0
while time.time()-start < max_time_allow and found == 0:
output = net_connect.send_command('show interface')
for line in output.split(os.linesep):
if re.search(("(?=.*FUlL|2way)(?=.*192.168.0.4)"),line , re.I):
print line
found = 1
time.sleep(2)
break
if found == 0:
print('TimeOut)https://stackoverflow.com/questions/45396016
复制相似问题