Python Telnet script

7.9k views Asked by At

Thanks to Python Library i was able to use their example to telnet to Cisco switches, I am using this for learning purposes, specifically learning python.

However, although all the code seem generally easy to read, I am a bit confused as to the following:

1- why use the if statement below 2- why use the "\n" after the username and password write method 3- why am i not getting the output on my bash terminal when the changes are infact committed and successful

HOST = "172.16.1.76"
user = raw_input("Enter your Telnet username : ")
password = getpass.getpass()

tn = telnetlib.Telnet(HOST) 

tn.read_until("Username: ") 
tn.write(user + '\n')                <----- 2
if password:                         <----- 1
    tn.read_until("Password: ")
    tn.write(password + "\n")        <------2
tn.write("show run \n") 

time.sleep(5)



output = tn.read_all()              <----- 3
print output  

print "=" * 30
print "Configuration Complete."

I am not sure as to why using the if statement above, typically once you input in the Username, you get the password prompt right afterward. why cant we just type :

tn.read_until("Username: ") 
tn.write(user + '\n')
tn.read_until("Password: ")
tn.write(password + "\n")

As for the second point, why use the '\n' after the passwords and username in the write method if we going to hit enter after we add them anyway?

3

There are 3 answers

2
Nullman On BEST ANSWER

1: the line

password = getpass.getpass()

asks you for you password, if you leave it empty, password will contain the empty string which, in an if statement, is the same as False
the script doesn't know ahead of time if you have a password on your server or not, it simulates knowing by asking you first and if you don't input anything, it assumes it doesn't (otherwise it would get stuck on tn.read_until("Password: ") forever.

2: the '\n' simulates you hitting the return key. when you enter your password, for example 'password<RETURN>' the variable password will not contain a trailing newline (\n), this is why it is manually appended

3: this one i dont know, possibly 5 seconds isn't enough time to wait

0
Zhi Yuan On

After execute tn = telnetlib.Telnet(HOST) you have created a telnet channel from your machine to HOST. But you still need to communicate with HOST to push/send your commands and receive the outputs.

To push your commands to HOST, you need to execute tn.write("your_commands_or_input \n"), \n means newline/return, which tells your current commands need to be executed now. After the execution, HOST return the result, which will be caught by your telnet object "tn" and saved in its "local cache", you can search any keywords you expected in this cache by using tn.read_until method, if the expected keyword has been found, read_until will stop(always stop on the 1st found), and you can do anything you need(It's your turn now), else the read_until will keep waiting the output from HOST(Haven't you turn yet). Finally if you want to check all output have been cached, you can execute tn.read_all().

Remember some of the HOST using different login output, i.e Username vs username or Password vs password, you better to use regular expression to match them.

0
Neo On

There is a python library on github, specifically for telneting to cisco devices.

pip install git+https://github.com/sergeyzelyukin/cisco-telnet.git

import ciscotelnet
with ciscotelnet.CiscoTelnet(host, verbose = False) as cisco:
  if cisco.login(final_mode=CiscoTelnet.MODE_ENABLE, user="john", user_pass="12345678", enable_pass="cisco"):
  # if cisco.login(final_mode=CiscoTelnet.MODE_ENABLE, line_pass="abcdef", enable_pass="cisco"):
    print cisco.cmd("sh int status | inc Fa0/1") 
    print cisco.conf(["interface fast0/1", "descr blank", "load-interval 300"])  
    print cisco.wr()