[英]How would I go about printing a line that reads “ input errors detected” based on the output of the sent command?
I am new to python and I am currently writing a python script that sends a command to a Cisco switch and returns specific lines. 我是python的新手,我正在编写一个python脚本,它将命令发送到Cisco交换机并返回特定的行。 The part I am having difficulties with is how would I print a line that states "input errors detected" based on the output of the command. 我遇到困难的部分是如何根据命令的输出打印一条表示“检测到输入错误”的行。 For example, if there 0 input errors in a line, it would do nothing but if the line has a value greater than zero input errors it would print "input errors detected"? 例如,如果一行中有0个输入错误,它什么都不做,但如果该行的值大于零输入错误,它将打印“检测到输入错误”? Thanks. 谢谢。
The command (ssh_session.send_command("sh interfaces | in up|CRC")) returns lines like these " 0 input errors, 0 CRC, 0 frame, 0 overrun, 0 ignored" 命令(ssh_session.send_command(“sh interfaces | in up | CRC”))返回如下行“0输入错误,0 CRC,0帧,0溢出,忽略0”
for device in (cisco1, cisco2):
ssh_session = netmiko.ConnectHandler(**device)
print("+++++ {0} +++++".format(device["ip"]))
output = (ssh_session.send_command("sh interfaces | in up|CRC"))
for line in output.splitlines():
if "input errors" in line:
print(line)
ssh_session.disconnect()
What you need to do is parse the output message. 您需要做的是解析输出消息。 Here's one way (there might be better ways): 这是一种方式(可能有更好的方法):
This assumes the structure of the output is what you have pasted above - might have a lot of edge cases. 这假设输出的结构是你上面粘贴的 - 可能有很多边缘情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.