cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
3783
Views
0
Helpful
5
Replies

Connecting to a router python

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'))

I try to connect to a router R1 with GNS3 using a Python script. But it still freeze because, I think, it can't connect to a router with a line 

tn = telnetlib.Telnet(HOST)

The connection to telnet from R1's terminal works fine

5 Replies 5

balaji.bandi
Hall of Fame
Hall of Fame

You can look the below thread and use other modules and check if that works :

https://www.9tut.net/knowledge/connect-python-to-gns3-for-automation-in-win10/3

https://gns3.com/community/featured/python-script-for-ssh-to-router

https://gns3.com/community/blog/network-automation-using-python-

 

BB

=====️ Preenayamo Vasudevam ️=====

***** Rate All Helpful Responses *****

How to Ask The Cisco Community for Help

Thank you ! I will look at it

Georg Pauwen
VIP Alumni
VIP Alumni

Hello,

 

try and add timers to the prompts (marked in bold😞

 

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: " , 5)
tn.write(user + "\n")
if password:
tn.read_until("Password: " , 5)
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'))

Hello, in fact I think it is the line 

tn = telnetlib.Telnet(HOST)

that doesn't work... 

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