How to print/display the output of a telnet session in terminal window (Python)

15,391

Solution 1

The short answer is to use read_until() with a timeout. e.g.

timeout = 3    # seconds
tn_read = tn.read_until('User Name :', timeout)
print repr(tn_read)

The long answer follows.

read_very_eager() is non-blocking and it will return any cooked data already available, but nothing if there isn't any data. If you call a non-blocking "read" method too soon after establishing the connection, there may be no data to read and read_very_eager() will return an empty string - ''.

So your problem is probably timing related; the read is non-blocking and can not return data that has not yet been received, processed, and buffered. When you interact via the terminal, it takes time for you to type in the commands, so you don't notice the timing issue, but when run from a script the human delay is removed, and the timing issue becomes apparent. Try sleeping before calling the read method:

import telnetlib
import time

HOST = "10.1.1.151"
tn = telnetlib.Telnet(HOST)
test_var = ["test"]
print test_var
time.sleep(5)
tn_read =  tn.read_very_eager()
print repr(tn_read)

Run the above as a script and you might see your expected output.... then again, you might not. Instead you might still see:

['test']
''

There may be other factors too, in particular the server may be responding with a Telnet IAC sequence. telnetlib will consume this sequence if it has arrived before you call read_very_eager() (any read function actually). In this case read_very_eager() also returns an empty string.

If you want to see what is being exchanged over the connection, you can call set_debuglevel(1):

import telnetlib

tn = telnetlib.Telnet('library.cedarville.edu')   # any old server will do
tn.set_debuglevel(1)
tn_read =  tn.read_all()
print repr(tn_read)

Typical output is:

Telnet(library.cedarville.edu,23): recv '\xff\xfd\x03'
Telnet(library.cedarville.edu,23): IAC DO 3
Telnet(library.cedarville.edu,23): recv '\xff\xfb\x03\xff\xfb\x01\xff\xfd\x18\xff\xfd#\xff\xfd$\xff\xfd\x1f'
Telnet(library.cedarville.edu,23): IAC WILL 3
Telnet(library.cedarville.edu,23): IAC WILL 1
Telnet(library.cedarville.edu,23): IAC DO 24
Telnet(library.cedarville.edu,23): IAC DO 35
Telnet(library.cedarville.edu,23): IAC DO 36
Telnet(library.cedarville.edu,23): IAC DO 31
Telnet(library.cedarville.edu,23): recv '\r\n'
Telnet(library.cedarville.edu,23): recv 'login: '

Of the other "read" functions, you should probably use read_until(). If you don't want to block indefinitely, pass a timeout to it as shown above.

Solution 2

Example of a telnet session on a terminal of a networking gear:

HOST = "1.1.1.1"
PORT = 30001
TIMEOUT = 3
PASSWD = "cisco"
COMMANDS = ["term len 0","show version","show ip route","show run","!!!end!!!"]

import telnetlib

tn = telnetlib.Telnet(HOST, PORT, TIMEOUT)
#tn.set_debuglevel(1)
tn.write("\r\n\r\n")
for cmd in COMMANDS:
    tn.write(cmd+"\r\n")

print tn.read_until("!!!end!!!\r\n",3)
tn.close()
Share:
15,391
flamenco
Author by

flamenco

R, Python, C, FPGA, Cisco, Data Mining, AI, Machine Learning...

Updated on June 05, 2022

Comments

  • flamenco
    flamenco almost 2 years

    Can someone help to understand how print in the terminal window works? Here is a test script

    test_script.py

    import telnetlib
    HOST = "10.1.1.151"
    tn = telnetlib.Telnet(HOST)
    tn.open(HOST)
    test_var = ["test"]
    print test_var
    tn_read =  tn.read_very_eager()
    print tn_read
    

    Output when script is run from terminal:

    $ python test_script.py
      ['test']
    

    tn_read should be something like "User Name : " but it is not printed in the terminal window.

    If I run it from the interpreter I get what is expected:

     >>> tn.read_very_eager()
     '\n\rUser Name : '
    

    Why or what needs to be done to have the following output when the script is called from the terminal ?

     $ python test_script.py
      ['test']
      User Name :  
    
  • tarabyte
    tarabyte over 6 years
    it was timing related when using read_very_eager! you saved me!