Search code examples
pythontelnetlib

Python telnetlib and console connection cisco node


I am trying to connect to the console connection of a cisco node via a cisco terminal/comm server. To do this I telnet to the IP-address of the cisco terminal/comm server on a specific port let us say X.X.X.X - port 2068. When I am doing this from my computer via the CLI it looks like this:

[user@computer]$ telnet X.X.X.X 2068
Trying X.X.X.X...
Connected to X.X.X.X (X.X.X.X).
Escape character is '^]'.

Username: <user>
Password: 

console-cisco-node>

So no problem via the CLI on my computer. But when I run the below Python code on my computer it does not seem to work ...

#! /usr/bin/env python

import telnetlib

tn = telnetlib.Telnet("X.X.X.X",2068)
tn.set_debuglevel(8)
data = tn.read_some()
tn.close()
if data == '':
  print 'variable data is EMPTY'
else:
  print data
  print "variable data is FILLED !!!"

When I run this code I only get to see this, it looks like the 'tn.read_some()' just waits forever because nothing is coming from the cisco terminal/comm server ? [same for tn.read_all()]

PS. I stopped the running code by hitting CTRL-C

[user@computer]$ ./test.py   
Telnet(X.X.X.X,2068): recv '\xff\xfb\x01\xff\xfb\x03\xff\xfd\x18\xff\xfd\x1f'
Telnet(X.X.X.X,2068): IAC WILL 1
Telnet(X.X.X.X,2068): IAC WILL 3
Telnet(X.X.X.X,2068): IAC DO 24
Telnet(X.X.X.X,2068): IAC DO 31
Telnet(X.X.X.X,2068): recv '\xff\xfc\x01'
Telnet(X.X.X.X,2068): IAC WONT 1
Telnet(X.X.X.X,2068): recv '\xff\xfc\x03'
Telnet(X.X.X.X,2068): IAC WONT 3
Telnet(X.X.X.X,2068): recv '\xff\xfe\x18'
Telnet(X.X.X.X,2068): IAC DONT 24
Telnet(X.X.X.X,2068): recv '\xff\xfe\x1f'
Telnet(X.X.X.X,2068): IAC DONT 31

Traceback (most recent call last):
  File "./test.py", line 7, in ?
    data = tn.read_some()
  File "/usr/lib64/python2.4/telnetlib.py", line 345, in read_some
    self.fill_rawq()
  File "/usr/lib64/python2.4/telnetlib.py", line 521, in fill_rawq
    buf = self.sock.recv(50)
KeyboardInterrupt

When I change the 'tn.read_some()' in the code to 'tn.read_eager()' or 'tn.read_very_eager()' or 'tn.read_lazy()' or 'tn.read_very_lazy()' and run the code again it shows me this:

[user@computer]$ ./test.py
variable data is EMPTY

When I change the python code to connect not to the console connection of the cisco node but to the management connection of the cisco node (another IP-address Y.Y.Y.Y on normal port 23) as follows it works just fine, I see this output:

[user@computer]$ ./test1.py 
Telnet(Y.Y.Y.Y,23): recv '\xff\xfb\x01\xff\xfb\x03\xff\xfd\x18\xff\xfd\x1f'
Telnet(Y.Y.Y.Y,23): IAC WILL 1
Telnet(Y.Y.Y.Y,23): IAC WILL 3
Telnet(Y.Y.Y.Y,23): IAC DO 24
Telnet(Y.Y.Y.Y,23): IAC DO 31
Telnet(Y.Y.Y.Y,23): recv '\r\n************************************************'

************************************************
variable data is FILLED !!!

So the Python code is OK I think. It is just that the cisco terminal/COMM server (X.X.X.X) is reacting in such a different way then normal that Python telnetlib is confused I think.

Anyone out there experienced something simular ?


Solution

  • It looks like on port 2068 you do not have a true telnet protocol. From telnet man When connecting to a non-standard port, telnet omits any automatic initiation of TELNET options. When the port number is preceded by a minus sign, the initial option negotiation is done.. But telnetlib supposes that if you use it, you want a telnet protocol. As you said it is not easy to use wireshark on the connection, you could try to do CLI telnet to port -2068 to verify that you get same problem than with telnetlib.

    Could it be an option to use a simple socket ?

    s = socket.create_connection(('X.X.X.X', 2068))
    data = s.recv(1024)
    ...