2

I am telneting to a Cisco router via Python3. However, it hangs up after running the script (But I am able to telnet to the router from my Linux bash). Please see the snippets of my script and outputs below.

import getpass
import telnetlib
HOST = "10.10.32.3"
user = input("Enter your telnet username: ")
password = getpass.getpass()
tn = telnetlib.Telnet(HOST)
tn.read_until(b"Username: ")
tn.write(user.encode('ascii') + b"\n")
if password:
    tn.read_until(b"Password: ")
    tn.write(password.encode('ascii') + b"\n")
tn.write(b"conf t\n")
tn.write(b"int l0\n")
print(tn.read_all().decode('ascii'))

And this is the output of debug telnet on the router

Router#
*Nov 2 23:48:24.317: Telnet578: 1 1 251 1
*Nov 2 23:48:24.318: TCP578: Telnet sent WILL ECHO (1)
*Nov 2 23:48:24.318: Telnet578: 2 2 251 3
*Nov 2 23:48:24.318: TCP578: Telnet sent WILL SUPPRESS-GA (3)
*Nov 2 23:48:24.318: Telnet578: 80000 80000 253 24
*Nov 2 23:48:24.319: TCP578: Telnet sent DO TTY-TYPE (24)
*Nov 2 23:48:24.319: Telnet578: 10000000 10000000 253 31
*Nov 2 23:48:24.319: TCP578: Telnet sent DO WINDOW-SIZE (31)
*Nov 2 23:48:24.383: TCP578: Telnet received DONT ECHO (1)
*Nov 2 23:48:24.383: TCP578: Telnet sent WONT ECHO (1)
*Nov 2 23:48:24.387: TCP578: Telnet received DONT SUPPRESS-GA (3)
*Nov 2 23:48:24.387: TCP578: Telnet sent WONT SUPPRESS-GA (3)
Router#
*Nov 2 23:48:24.389: TCP578: Telnet received WONT TTY-TYPE (24)
*Nov 2 23:48:24.389: TCP578: Telnet sent DONT TTY-TYPE (24)
*Nov 2 23:48:24.390: TCP578: Telnet received WONT WINDOW-SIZE (31)
*Nov 2 23:48:24.391: TCP578: Telnet sent DONT WINDOW-SIZE (31)
*Nov 2 23:48:24.407: TCP578: Telnet received DONT ECHO (1)
*Nov 2 23:48:24.407: TCP578: Telnet received DONT SUPPRESS-GA (3)
*Nov 2 23:48:24.407: TCP578: Telnet received WONT TTY-TYPE (24)
*Nov 2 23:48:24.408: TCP578: Telnet received WONT WINDOW-SIZE (31)

And the output of show tcp brief

Router#sho tcp brief 
TCB       Local Address               Foreign Address             (state)
10C90CE0  10.10.32.3.23              192.168.122.61.51466        ESTAB

I can create the loopback interface, but my Linux bash doesn't show me the telnet output. Please guide accordingly. Thank you.

Jones Doe
  • 51
  • 1
  • 6

4 Answers4

3

.read_all() in telnetlib is documented as "Read all data until EOF; block until connection closed.". Since you've done nothing that will result in the connection being closed, a hang is exactly what you should expect to happen here. Try sending an exit command to the router first. Or, if you intend to issue further commands based on the results you read, use .read_until() (possibly with a timeout specified) instead.

jasonharper
  • 9,450
  • 2
  • 18
  • 42
3

Telnet.read_all() : Read all data until EOF; block until connection closed. So, you must indicate that it is the End Of File by using exit command. Your code should be like

import getpass
import telnetlib
HOST = "10.10.32.3"
user = input("Enter your telnet username: ")
password = getpass.getpass()
tn = telnetlib.Telnet(HOST)
tn.read_until(b"Username: ")
tn.write(user.encode('ascii') + b"\n")
if password:
    tn.read_until(b"Password: ")
    tn.write(password.encode('ascii') + b"\n")
tn.write(b"conf t\n")
tn.write(b"int l0\n")
tn.write("end\n")
tn.write("exit\n")
print(tn.read_all().decode('ascii'))
Mahmoud Anwer
  • 164
  • 1
  • 2
  • 12
  • I was closing the telnet connection before giving exit or logout command, Finally i understand read_all now.. – R__raki__ May 31 '19 at 09:32
1

You need to send the command "terminal length 0" before others. This is my script to get the config:

import getpass
import telnetlib

HOST = "192.168.0.X"
user = input("Enter your username: ")
password = getpass.getpass()

#Iniciando sesion telnet
tn = telnetlib.Telnet(HOST)

tn.read_until(b"Username: ")
tn.write(user.encode('ascii') + b"\n")
if password:
    tn.read_until(b"Password: ")
    tn.write(password.encode('ascii') + b"\n")

tn.write(b"terminal length 0\n")
tn.write(b"show runn\n")
tn.write(b"exit\n")

print(tn.read_all().decode('ascii'))
0

Your code is Correct but should change some lines like this and important one is last line :

Be sure that in your Router you configured :

  • aaa new-model
  • aaa authentication login default local
  • line vty 0 15
  • transport input all
  • login authentication default
import getpass
import telnetlib

HOST = "10.10.32.3"
user = input("Enter your telnet username: ")

password = getpass.getpass()
tn = telnetlib.Telnet(HOST)
tn.read_until(b"Username: ")
tn.write(user.encode('ascii') + b"\n")
if password:
    tn.read_until(b"Password: ")
    tn.write(password.encode('ascii') + b"\n")
# Be sure that if you configured enable password you should use this section :
tn.write(b"enable\n")
tn.write(b"cisco\n")
# If you did not use enable password start from this section
tn.write(b"conf t\n")
tn.write(b"int l0\n")
# sample config that you would be sure that you changed your configuration
tn.write(b"des THIS IS JUST FOR TEST\n")
tn.write(b"end\n")
tn.write(b"exit\n")
# **End section should be exactly like this line**
print(tn.read_all())