cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Announcements
Join Customer Connection to register!
407
Views
0
Helpful
5
Replies
MariiaAgeeva1030
Beginner

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
VIP Expert

Thank you ! I will look at it

Georg Pauwen
VIP Expert

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