Python / Netmiko:在交换机日志中查找故障
Python / Netmiko: Look for failures in switch log
我正在尝试让我的程序查看来自交换机的日志,看看其中是否出现 "words" 字样。
从交换机中提取日志的部分有效,但检查坏词的部分无效。
我能够使代码适用于 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__
有谁知道如何让代码查看开关的输出,因为它还没有成为文件?
# 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
我正在尝试让我的程序查看来自交换机的日志,看看其中是否出现 "words" 字样。
从交换机中提取日志的部分有效,但检查坏词的部分无效。 我能够使代码适用于 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__
有谁知道如何让代码查看开关的输出,因为它还没有成为文件?
# 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