I am using telnetlib for simple telnet script to Juniper switch. Below is my code:
import telnetlib
HOST = raw_input("Enter host IP address: ") 开发者_如何学Python
USER = raw_input("Enter Username: ")
PWD = raw_input("Enter Password: ")
TNT = telnetlib.Telnet(HOST, 23, 10)
TNT.read_until("login:")
TNT.write(USER.encode('ascii') + "\n")
TNT.read_until("Password:")
TNT.write(PWD.encode('ascii') + "\n")
TNT.write("set cli screen-length 10000\nconfigure\nshow\nexit\n")
print (TNT.read_all().decode('ascii'))
TNT.close()
raw_input ("Press any Key to Quit: ")
Whenever I run this program with Juniper switch it gives me this error:
Traceback (most recent call last):
File "D:\Python\AuTel Project\Old versions and tials\Telnet (Python 2.7) V1.4.py", line 17, in <module>
print (TNT.read_all().decode('ascii'))
File "C:\Python27\lib\telnetlib.py", line 325, in read_all
self.fill_rawq()
File "C:\Python27\lib\telnetlib.py", line 516, in fill_rawq
buf = self.sock.recv(50)
timeout: timed out
I have faced this problem before with Cisco and Nortel, but I could overcome it with "terminal lenght 0" command on Cisco and similar comand on Nortel. I tried to use the equivalent command on Juniper (set cli screen-length), but I am still getting the same error. I need to know what is the meaning of this error and what is the reason of it, and how to overcome it.
Best Regards,
The error message
buf = self.sock.recv(50)
timeout: timed out
is pretty obvious.
Your connection timed out for whatever reason.
Either some firewall or network component in between closed the connection due to inactivity after some time or the remote service did not respond within a reasonable amout of time.
I had the same problem.
Command to change "TNT.read_all()" -> "TNT.read_some()" and script to retry.
Try to exit from terminal completely, I had similar issue same, error message after to quit script. The issue was, I was getting to enable mode and not exiting from terminal. Once I added Exit
as last command to exit telnet script, it worked.
This was error I was getting, after I keyed ctr+c
^CTraceback (most recent call last):
File "./python_ex1_telnet_reading_file_1.py", line 44, in <module>
tn.write(b"exit\n")
File "/usr/lib/python3.8/telnetlib.py", line 335, in read_all
self.fill_rawq()
File "/usr/lib/python3.8/telnetlib.py", line 526, in fill_rawq
buf = self.sock.recv(50)
Snipped from script:
tn.write(b"end\n")
tn.write(b"exit\n") -- This was missing
print(tn.read_all().decode('ascii'))
This worked very well for me.
tn.read_very_eager()
Remember, we need to provide sufficient sleep time before this, so that it will be recorded before reading.
精彩评论