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

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

5 REPLIES 5
Highlighted
VIP Mentor

Re: Python telnet

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 ***
Highlighted
Participant

Re: Python telnet

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".
Highlighted
Beginner

Re: Python telnet

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

 

Highlighted
Participant

Re: Python telnet

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

 

Highlighted
Beginner

Re: Python telnet