Python telnet
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-22-2020 02:06 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-22-2020 05:15 AM
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) ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-22-2020 11:03 AM
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".
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-06-2020 02:08 PM - edited 05-06-2020 02:10 PM
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'))
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-07-2020 11:07 PM - edited 05-07-2020 11:08 PM
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")
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-01-2021 07:30 AM
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")
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-06-2020 02:09 PM - edited 05-06-2020 02:11 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-18-2021 07:32 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-15-2022 12:14 PM
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'))
