我试图让我的程序从交换机查看日志,看看其中是否出现“单词”。
从交换机中提取日志的部分有效,但检查坏词的部分无效。我能够使代码适用于 txt 文件,但不适用于输出。
我的代码:
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
这是我得到的错误:
Traceback (most recent call last):
File "script.py", line 150, in <module>
with textring as infile:
AttributeError: __enter__
有谁知道如何让代码查看开关的输出,因为它还没有变成一个文件?