如何将telnet输出日志保存到文本文件中



嗨,伙计们,我如何将输出日志保存到该脚本的文本文件中,以解决这个问题

import sys
import telnetlib
import getpass
#####################
###### Define Host
host=["192.168.1.164","192.168.1.169"]
devno=len(host)
user='ali'
Password='cisco'
for i in range(devno):
print('*'*50 + ' Connected to device : '+str(host[i]) +'*'*50)
tn=telnetlib.Telnet(host[i])
tn.read_until(b'Username: ')
tn.write(user.encode('ascii')+ b'n')
tn.read_until(b'Password')
tn.write(Password.encode('ascii')+b'n')
#tn.write(b'enable' + b'n')
#tn.write(b"n")
#tn.write(b"terminal length 0n")
#tn.write(b"show runn")
#tn.write(b' show ip routen')
tn.write(b'  exitn')
print(tn.read_all().decode('ascii'))

您可以将其添加到for循环的末尾

with open("text1.txt", "w") as f:
f.write(str(tn.read_all().decode("ascii")))

以上应该将您的命令输出保存到文件中

如果你也想获得日志,那么你需要在退出之前添加以下内容

tn.write(b' show logn')

最新更新