简体   繁体   English

Python / Netmiko:在交换机日志中查找故障

[英]Python / Netmiko: Look for failures in switch log

I am trying to make my program look at the log from the switch and see if the words "words" appear in it.我试图让我的程序查看来自交换机的日志,并查看其中是否出现“单词”一词。

The part that pulls the log from the switch works, but the part that checks for bad words doesn't.从交换机中提取日志的部分工作,但检查坏词的部分没有。 I was able to make the code work for a txt file, but not for the output.我能够使代码适用于 txt 文件,但不适用于输出。

My code:我的代码:

    net_connect.enable()
    output = net_connect.send_command('show log')
    print(output)
    filename = ('log_files/SW_'+ip_addresses[count]+'_'+TNOW+'.txt')

    SAVE_FILE = open(filename, 'w+')
    SAVE_FILE.write(output)

    print(filename)
    textring = (output)

    with textring as infile:
        text = infile.read()
    words = ['flapping', 'Unexpected', 'down']
    for word in words:
        if word in text:
            print('success')
            with open('warnings/SW_WARNING.txt', 'w+') as save_file:
                save_file.write(text)
            break

This is the error I get:这是我得到的错误:

    Traceback (most recent call last):
    File "script.py", line 150, in <module>
    with textring as infile:
    AttributeError: __enter__

Does anyone know how to make the code look at the output of the switch, as it has not become a file yet?有谁知道如何让代码查看开关的输出,因为它还没有成为文件?

# Eliminate as unnecessary/error on your "with" statement
# textring = (output)
# with textring as infile:
#    text = infile.read()

for word in ['flapping', 'Unexpected', 'down']:
    if word in output:
        print('success')
        with open('warnings/SW_WARNING.txt', 'w+') as save_file:
            save_file.write(output)
        break

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

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