Reputation: 11
Recently i have started learning python, and currently working on a small script to remote telnet from physical laptop to a router in GNS. Every connectivity is fine and i am able to telnet to the router via CMD using telnet
command.
But when i am running my script instead of direct telnet command that where i am facing issue. Program runs fine without any issue, but telnet does not work.
I tried running debug as well on router can someone please check the script code and output from the debug command and advise what could be possible wrong.
SCRIPT CODE:
import getpass
import sys
import telnetlib
HOST = "192.168.1.7"
tn =telnetlib.Telnet("192.168.1.7", "23")
user = input("Enter your username: ")
password = getpass.getpass()
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")
OUTPUT OF DEBUG TELNET COMMAND ON CISCO:
R2#
*Aug 6 16:50:15.095: Telnet2: 1 1 251 1
*Aug 6 16:50:15.095: TCP2: Telnet sent WILL ECHO (1)
*Aug 6 16:50:15.095: Telnet2: 2 2 251 3
*Aug 6 16:50:15.095: TCP2: Telnet sent WILL SUPPRESS-GA (3)
*Aug 6 16:50:15.095: Telnet2: 80000 80000 253 24
*Aug 6 16:50:15.099: TCP2: Telnet sent DO TTY-TYPE (24)
*Aug 6 16:50:15.099: Telnet2: 10000000 10000000 253 31
*Aug 6 16:50:15.099: TCP2: Telnet sent DO WINDOW-SIZE (31)
R2#
*Aug 6 16:50:23.451: TCP2: Telnet received DONT ECHO (1)
*Aug 6 16:50:23.451: TCP2: Telnet sent WONT ECHO (1)
*Aug 6 16:50:23.539: TCP2: Telnet received DONT SUPPRESS-GA (3)
*Aug 6 16:50:23.539: TCP2: Telnet sent WONT SUPPRESS-GA (3)
*Aug 6 16:50:23.543: TCP2: Telnet received WONT TTY-TYPE (24)
*Aug 6 16:50:23.543: TCP2: Telnet sent DONT TTY-TYPE (24)
*Aug 6 16:50:23.543: TCP2: Telnet received WONT WINDOW-SIZE (31)
*Aug 6 16:50:23.547: TCP2: Telnet sent DONT WINDOW-SIZE (31)
*Aug 6 16:50:23.567: TCP2: Telnet received DONT ECHO (1)
*Aug 6 16:50:23.571: TCP2: Telnet received DONT SUPPRESS-GA (3)
*Aug 6 16:50:23.571: TCP2: Telnet received WONT TTY-TYPE (24)
R2#
*Aug 6 16:50:23.571: TCP2: Telnet received WONT WINDOW-SIZE (31)
R2#
Upvotes: 0
Views: 1612
Reputation: 48
I don't have the rest of your Python script. But if you used .read_all()
and you have no further commands to issue, you have to send exit
command to the router
as .read_all()
"reads all data until EOF as bytes; block until connection closed." (https://docs.python.org/3.1/library/telnetlib.html).
import getpass
import telnetlib
HOST = "192.168.1.7"
tn =telnetlib.Telnet("192.168.1.7", "23")
user = input("Enter your username: ")
password = getpass.getpass()
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"exit\n") # This line added if you don't have in your script
print(tn.read_all().decode('ascii'))
If you have further commands based on the output you receive, you may use: read_until(expected, timeout=1)
Read until a given byte string, expected, is encountered or until timeout seconds have passed.
Upvotes: 1
Reputation: 37163
First, you need to know where your program is stopping - otherwise how can you know which statements are succeeding and which are failing?
For debugging purposes, insert a print
call (or statement, if Python 2) after each statement, like
tn =telnetlib.Telnet("192.168.1.7", "23")
user = input("Enter your username: ")
password = getpass.getpass()
print"About to call tn.read")
tn.read_until(b"Username: ")
print"About to write username")
tn.write(user.encode("ASCII") + b"\n")
if password:
print"Looking for password prompt")
tn.read_until(b"Password: ")
print"About to write password")
tn.write (password.encode("ASCII") + b"\n")
One final thought: on the Cisco routers I used many years ago, the username prompt wouldn't appear until you'd entered a newline - so you could try adding a tn.write(b'\n')
before the first tn.read_until
call.
Upvotes: 0