10-18-2020 11:00 PM
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#
12-16-2022 05:34 PM
from getpass import getpass
import sys
import telnetlib
HOST = "192.168.73.131"
user = input("Enter your telnet username: ")
password = getpass("Enter password")
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"configure t \n")
tn.write(b"int loop 6 \n")
tn.write(b"ip address 6.6.6.6 255.255.255.255 \n")
tn.write(b"end\n")
tn.write(b"exit\n")
print(tn.read_all().decode('ascii'))
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide