Good day! I am writing a script for conducting frauds with a router via telnet. I use the following code:

import telnetlib user = 'admin' password = 'admin' PROMPT = '>' tn = telnetlib.Telnet(host = 'xx.xx.xx.xx', port = 23, timeout = 5) print(tn.read_all()) tn.read_until(b'login: ') tn.write(user + '\n') if password: tn.read_until('Password: ') tn.write(password + '\n') tn.read_until(PROMPT) tn.write('ls\n') print (tn.read_until(PROMPT)) tn.write('exit\n') 

However, at startup, after 5 seconds the script crashes with an error

 Traceback (most recent call last): File "C:/Users/vladF/PycharmProjects/untitled/testtelnet", line 6, in <module> print(tn.read_all()) File "C:\Users\vladF\AppData\Local\Programs\Python\Python35\lib\telnetlib.py", line 333, in read_all self.fill_rawq() File "C:\Users\vladF\AppData\Local\Programs\Python\Python35\lib\telnetlib.py", line 524, in fill_rawq buf = self.sock.recv(50) socket.timeout: timed out 

    1 answer 1

     import telnetlib user = 'admin' password = 'admin' PROMPT = '>' tn = telnetlib.Telnet(host = 'xx.xx.xx.xx', port = 23, timeout = 5) tn.read_until(b'login: ') tn.write(user + '\n') tn.read_until('Password: ') tn.write(password + '\n') tn.read_until(PROMPT) tn.write('ls\n') print (tn.read_until(PROMPT)) tn.write('exit\n') 

    tn.read_all () cannot be called until the connection is broken. tn.close () or tn.write (b'logout \ r ')