cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
14811
Views
5
Helpful
8
Replies

Python telnet

Hello Gents,

 running below python 2 script in GNS3 ..telnet is successful but not configure IP address as I mentioned in the script...whats the issue?

 

import getpass
import sys
import telnetlib

HOST = "192.168.122.71"
user = raw_input("Enter your username ")
password = getpass.getpass()

tn = telnetlib.Telnet(HOST)

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

tn.write("enable\n")
tn.write("configure t\n")
tn.write("interface loopback0\n")
tn.write("ip address 1.1.1.1 255.255.255.255 \n")
print tn.read_all()

 

 

 

& download python, go,perl, appllince in GNS3 but only running python 2 how to install python 3 in Appliance.?

Thank in advance

8 Replies 8

balaji.bandi
Hall of Fame
Hall of Fame

check manually is the user works, with given information - the user has enabled access?

 

I do not see any issue with the script here. ( what is the logs you get when you run the script) ?

 

 

BB

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

How to Ask The Cisco Community for Help

ngkin2010
Level 7
Level 7
Hi,

I didn't test the script, but as @balaji.bandi said, please post the error & the program's output.

I have observed one problem from your script is in the following line:

user = raw_input("Enter your username ")

You may need to strip out the newline character inputted by user.

user = raw_input("Enter Your username ")
user = user.rstrip()

==

For the python 3 in GNS3, it should be already included. You may simply call it by "python3".

i cant telnet, it hanged for10 mins then giving me below error

 

root@NetworkAutomation-1:~# python3 telnettoswitch.py
Enter your telnet username: cisco
Password:
Traceback (most recent call last):
File "telnettoswitch.py", line 13, in <module>
tn.read_until(b"Password: ")
File "/usr/lib/python3.8/telnetlib.py", line 329, in read_until
return self.read_very_lazy()
File "/usr/lib/python3.8/telnetlib.py", line 405, in read_very_lazy
raise EOFError('telnet connection closed')
EOFError: telnet connection closed
root@NetworkAutomation-1:~#

 

 

Below is the script 

 

import getpass
import telnetlib

HOST = "192.168.122.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"enable \n")
tn.write(b"passme \n")


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

 

Your telnet session was closed before reaching the line 13.

 

The reason is because your script have inputted wrong password to the switch (even you have inputted the correct password).

 

Please find the explanation as below:

 

import getpass
import telnetlib

HOST = "192.168.122.3"
user = input("Enter your telnet username: ")
// After this line of code has executed, you read user's input (including the newline char: \n)
// Meaning that your 'user' variable is something like user = "username\n"

// You need to remove the unwanted tailing \n by adding the following code
user = user.rstrip()

password = getpass.getpass() tn = telnetlib.Telnet(HOST) tn.read_until(b"username: ")
tn.write(user.encode('ascii') + b"\n")
//Otherwise, when your script passing the variable 'user' with your additional tailing b"\n"
//which will become "username\n\n"
//which mean you hit the enter twice, in other word, you enter an empty password to the switch.

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

 

Did not work...showing the same error!!

 

tn.read_until(b"Password: ")
File "/usr/lib/python3.8/telnetlib.py", line 329, in read_until
return self.read_very_lazy()
File "/usr/lib/python3.8/telnetlib.py", line 405, in read_very_lazy
raise EOFError('telnet connectio")

 

DeanMoore
Level 1
Level 1

If you follow the console on GNS3 for the device while the script is running it may give you some clues,  only thing may be the space after the subnet mask I can see

 

Dean

tawa-ndafa
Level 1
Level 1

Try below:

 

root@NetworkAutomation-1:~# cat script1
import getpass
import telnetlib

HOST = "Router IP address"
user = input("Enter your telnet username: ")
user = user.rstrip()
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"enable\n")
tn.write(b"cisco\n")
tn.write(b"conf t\n")
tn.write(b"int l0\n")
tn.write(b"ip address 1.1.1.1 255.255.255.255\n")
tn.write(b"end\n")
tn.write(b"exit\n")

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