上次写了用Python做设备巡检2019-06-14 Python Paramiko模块 在Huawei OLT巡检实例
那么,输出的数据怎么处理?
举例,在每台OLT上输出数据类似下面这样:
ol01.xxx.lon#display ont info 0 all | no-more | include 48575443E8E0BD9D
{ <cr>||<K> }:
Command:
display ont info 0 all
It will take a long time if the content you search is too much or the string you input is too long,
you can press CTRL_C to break
0/ 1/0 2 48575443E8E0BD9D active online normal match no
我只想要最后一行,就是
0/ 1/0 2 48575443E8E0BD9D active online normal match no
写个函数来处理:
#定义示例字符串
st = '''ol01.xxx.lon#
display ont info 0 all | no-more | include 48575443E8E0B89D
{ <cr>||<K> }:
Command:
display ont info 0 all
It will take a long time if the content you search is too much or the string
you input is too long, you can press CTRL_C to break
0/ 1/0 18 48575443E8E0B89D active online normal match no
'''
def find_status(inp):
for line in inp.split('\n'):
if not 'active' in line:
continue
print(line)
break
return(line)
find_status(st)
这样输出的就是最后一行我要的数据。
再写一个for循环,把输出数据追加到一个文本里面。
filename = "All_result.txt"
with open(filename, "a") as f:
f.write(find_status(Result))
f.write('\n')
这样,运行完之后就会产生一个内容类似这样的文本:
0/ 2/1 12 48575443E8E0C79D active online normal match no
0/ 2/1 47 48575443E8E12E9D active online normal match no
0/ 2/1 48 48575443E8E1659D active online normal match no
0/ 2/1 49 48575443E8E0A99D active online normal match no
0/ 2/1 65 48575443E8E0AA9D active online normal match no
.....