cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
2003
Views
0
Helpful
2
Replies

A Telnetlib Challenge: the telnet server does not require login credentials

googleboy
Level 1
Level 1

The following python script connects you via telnet protocol to the specified telnet server. It works fine as long as the telnet server prompts for "Username: " and "Password: ". However, If the telnet server does not ask for credentials, this python script will fail. 

 

The question: How would fix the script so that it will also work when the remote telnet server does not ask for username or password?

 

Below is the python script:

import telnetlib
import getpass

HOST = "192.168.0.101"
user = input("Enter your username: ")
password = getpass.getpass()

tn = telnetlib.Telnet(HOST)

tn.read_until(b"Username: ")
tn.write(user.encode() + b"\n")


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


tn.write(b"terminal length 0\n")
tn.write(b"show version\n")
tn.write(b"exit\n")


output = tn.read_all()
print(output.decode())

 

2 Replies 2

balaji.bandi
Hall of Fame
Hall of Fame

If the remote server does not require login credentials, then you need to change the script based on the input method.

 

try using your normal tool putty and secure CRT, and show us how you able to connect to the device ?

 

BB

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

How to Ask The Cisco Community for Help

What about an "if" statement that checks to see if the remote telnet server is requesting a username and a password?


@balaji.bandi wrote:

If the remote server does not require login credentials, then you need to change the script based on the input method.

 

try using your normal tool putty and secure CRT, and show us how you able to connect to the device ?