3

I try to connect to a router R1 with GNS3 using a Python script:

import getpass
import sys
import telnetlib

HOST = "192.168.1.1"
user = raw_input("Enter your remote account: ")
password = getpass.getpass()

tn = telnetlib.Telnet(HOST)

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

tn.write("enable\n")
tn.write("cisco\n")
tn.write("conf t\n")
tn.write("exit\n")
tn.write("exit\n")


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

But it still freeze because, I think, it can't connect to a router with a line tn = telnetlib.Telnet(HOST)

When I do a ^C I have this error:

admin ~/Desktop $ python3 test.py
Enter your remote account: david
Password: 
^CTraceback (most recent call last):
  File "test.py", line 9, in <module>
    tn = telnetlib.Telnet(HOST)
  File "/usr/local/Cellar/python/3.6.5_1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/telnetlib.py", line 218, in _init_
    self.open(host, port, timeout)
  File "/usr/local/Cellar/python/3.6.5_1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/telnetlib.py", line 234, in open
    self.sock = socket.create_connection((host, port), timeout)
  File "/usr/local/Cellar/python/3.6.5_1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/socket.py", line 713, in create_connection
    sock.connect(sa)
KeyboardInterrupt

The connection to telnet from R1's terminal works fine

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
Nataila
  • 41
  • 6
  • The error message is standard when you press ^C and has nothing to do with any problems in your code. Are you able to connect to the router with `telnet` from the command line? – Code-Apprentice Jul 20 '20 at 08:29
  • @Code-Apprentice yes, It works well – Nataila Jul 20 '20 at 08:31
  • Don't you have to specify a port number ? Default port is 0 which may not be valid for this connection. – Vijay Jul 20 '20 at 09:24
  • One thing that will cause further problems (but doesn’t solve your *current* problem) is that you’re using strings instead of byte sequences to communicate with Telnet. You need to replace them all with byte sequences! – Konrad Rudolph Jul 20 '20 at 09:25
  • @Vijay I thought so too (the documentation is unclear) but the default port is internally replaced by port 23. – Konrad Rudolph Jul 20 '20 at 09:25
  • Try specifying the port you need to connect to. For example if port is 23, `tn = telnetlib.Telnet(HOST, 23)` and make sure you can telnet it via command line using `telnet 192.168.1.1 23` – Vijay Jul 20 '20 at 09:29
  • @Vijay `telnet 192.168.1.1 23` nope, it doesn't work – Nataila Jul 20 '20 at 09:42
  • What is the output of `sh ip int brief` on R1 ? Found a tutorial which is trying to do similar thing https://gns3.teachable.com/courses/python-for-network-engineers-netmiko-napalm-pyntc-telnet-ssh-and-more-learn-to-code/lectures/2925571 – Vijay Jul 20 '20 at 10:27
  • @Vijay I tried to look for the state of port 23 with `nmap` on mu terminal. It is closed. I use Mac so I don't know if there is a solution – Nataila Jul 20 '20 at 11:26

1 Answers1

0

Try this code:

import getpass
import sys
import telnetlib

HOST = "192.168.1.1"
PORT = 23

user = raw_input("Enter your remote account: ")
password = getpass.getpass()

tn = telnetlib.Telnet(HOST, PORT)

tn.read_until(b"Username: ")
tn.write(bytes(user + "\n", 'utf-8'))
if password:
    tn.read_until(b"Password: ")
    tn.write(bytes(password + "\n", 'utf-8'))

commands="enable\n cisco\n conf t\n exit\n exit\n"
tn.write(commands.encode())

print(tn.read_all())

This code will work if you have configured your router properly.

nvt_dc
  • 133
  • 1
  • 9