繁体   English   中英

如何根据发送命令的输出打印读取“检测到输入错误”的行?

[英]How would I go about printing a line that reads “ input errors detected” based on the output of the sent command?

我是python的新手,我正在编写一个python脚本,它将命令发送到Cisco交换机并返回特定的行。 我遇到困难的部分是如何根据命令的输出打印一条表示“检测到输入错误”的行。 例如,如果一行中有0个输入错误,它什么都不做,但如果该行的值大于零输入错误,它将打印“检测到输入错误”? 谢谢。

命令(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()

您需要做的是解析输出消息。 这是一种方式(可能有更好的方法):

  • 在输出中找到“输入错误”的出现; 如果没有找到 - 没有错误
  • 获取“输入错误”之前的输出的子字符串
  • 将此子字符串解析为整数
  • 如果解析的整数> 0,那么您可以打印“发现的错误”

这假设输出的结构是你上面粘贴的 - 可能有很多边缘情况。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM