如果在内 for 内,同时循环多个条件和计时器 python



我想(重复(读取输出命令的每一行,如果条件匹配打印行,如果不保持,则重复循环直到超时并打印当前输出或消息

在两种情况下,它都会保持循环打印行或超时

我只想打印一次行或在超时时超时一次,并且在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()

将 while 循环更改为在达到时间或执行打印语句时停止

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)

最新更新