简体   繁体   English

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

[英]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): 这是一种方式(可能有更好的方法):

  • Find the occurrence of "input errors" in the output; 在输出中找到“输入错误”的出现; if not found - no errors 如果没有找到 - 没有错误
  • Get the substring of the output that precedes "input errors" 获取“输入错误”之前的输出的子字符串
  • Parse this substring as an integer 将此子字符串解析为整数
  • If the parsed integer is > 0 then you can print "errors found" 如果解析的整数> 0,那么您可以打印“发现的错误”

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.

相关问题 您 go 如何根据输入值和 Python 中的输入值位置打印值? - How would you go about printing a value based the input value and the input value location in Python? 如何在大型文本文件中打印最后一行? - How would I go about printing the last line in a large text file? 我将如何在类似程序的命令行上进行参数设置? - How would I go about making an argument on my command-line like program? 我将如何根据 python 中的用户输入访问 CSV 中的特定元素? - How would I go about accessing a specific element in a CSV based off of user input in python? 我将如何模拟对非活动窗口的直接输入? - How would i go about simulating direct input to an inactive window? 我将如何同时查找多个整数,然后根据这些整数执行打印命令 - How would I go about finding multiple integers at the same time, then executing a print command based off those integers 在比较一个字符串输入是否是第二个字符串输入的前缀时,我将如何 go? - How would I go about in comparing whether one string input is a prefix of the second string input? 我将如何 go 创建一个从用户端重新启动代码的输入输入? - How would I go about creating an input input that restarts the code from the users end? Python-我将如何去做? - Python - How would I go about doing this? 我将如何 go 关于重命名 pandas 中的列名,在我创建列的同一行中 - How would I go about renaming a column name in pandas, in the same line that I made a column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM