2
回答
python telnet 获取返回值
华为云实践训练营,热门技术免费实践!>>>   

python 版本2.7的。

我用python谢了一个获取telnet返回值的程序,在read_all的时候没有返回值。求帮助。

#Telnet RouterOs & Run Command v0.1
#python v3.2
#Routeros v3.27
import telnetlib,time,os
#config_user_password_port_etc.
HOST='xxxxxxxxxxxx'
PORT='xxx'
user= 'xxxx'
password = 'xxxxxxxxx'
command_1 ='help'
command_2 ='quit'
 
tn = telnetlib.Telnet(HOST,PORT,30)


#tn = telnetlib.Telnet(HOST)
#input user
##tn.read_until(b"Login: ")
## tn.write(user.encode('UTF-8') + b"\n")
#input password
tn.read_until(b"Password: ")
tn.write(password.encode('UTF-8') + b"\n")
tn.read_until(b"S2> ")
tn.write("en\r\n")
tn.read_until(b"Password: ")
tn.write(password.encode('UTF-8') + b"\n")
#run command
tn.read_until(b'>')
tn.write(command_1.encode('UTF-8')+b"\r\n")
time.sleep(10)
tn.read_until(b'>')
tn.write(command_2.encode('UTF-8')+b"\r\n")
 
#write result in files
#print(tn.read_all)
result=tn.read_all()
file_object=open('result.txt','wb')
file_object.write(result)
file_object.close()
print ('Finish')
 
tn.close()



举报
eagle_91
发帖于3年前 2回/1K+阅
顶部