我是python的新手。我必须从telneting一个“被测设备”中获取一张表,并使用python在日志文件中获取它,而我确实使用下面的程序
import os
import sys
import getpass
import re
import telnetlib
HOST = "ip of host"
user = raw_input("Enter your remote account: ")
password = raw_input("Enter the password: ")
tn = telnetlib.Telnet(HOST)
tn.read_until("login: ")
tn.write(user + "\n")
tn.read_until("Password: ")
tn.write(password + "\n")
tn.read_until("*>")
tn.write("command-to-be-executed\n")
tn.write("exit\n")
output = tn.read_all()
f= open("C:/Python27/Scripts/logfile/module_show.txt" , "w+")
f.write(output)
f.close()
tn.close()
现在,我必须读取提取的日志文件,并根据第一列和最后一列打印以下特定行:
| ER | 2 | 252 | wPWs_sTE1-3-2 | | 3.3.3.3 | 30002 | 30002 | sUMoF |
| ER | 1 | 10251 | wPWd_sTE1-3-1 | | 3.3.3.3 | 100000 | 32768 | dUMoF |
我为此编写了一个单独的程序,但这不起作用。请参阅下面的程序:
import re
regex = re.compile("^(\|ER)")
with open("C:/Python27/Scripts/logfile/module_show.txt") as f:
for line in f:
result = regex.search(line)
if result:
print line
f.close()
请帮我解决同样的问题吗?
谢谢大家
经过更多的思考后,我发现了这一点,在实施regexp之前,我没有回过头来思考:
import os
import sys
import getpass
import re
import telnetlib
HOST = "ip address"
user = raw_input("Enter your remote account: ")
password = raw_input("Enter the password: ")
tn = telnetlib.Telnet(HOST)
tn.read_until("login: ")
tn.write(user + "\n")
tn.read_until("Password: ")
tn.write(password + "\n")
tn.read_until("*>")
tn.write("command to be executed\n")
tn.write("exit\n")
output = tn.read_all()
f= open("C:/Python27/Scripts/logfile/module_show.txt" , "w+")
f.write(output)
f.close()
tn.close()
regex = re.compile("(F\s\|\r)$")
with open("C:/Python27/Scripts/logfile/module_show.txt") as f:
for line in f:
result = regex.search(line)
if result:
print line
f.close()
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句