[Tutor] Telnet to cisco device

David Heiser David.Heiser at intelliden.com
Tue Feb 28 09:27:28 CET 2006


 
The following works with the Cisco switch that I have available. Cisco
4006 with CatOS 6.2. It may not work with your model/OS or your switch
may be configured differently. For instance, the default prompt
terminates with "(enable)" so I used "tn.read_until(')')" instead of
"tn.read_until(HOST + '#')".
 
----------------------------------------------
import getpass 
##import sys 
import telnetlib 
 
##HOST = 'switch_name'  # this is the hostname for device, to be changed
to read from file when figure that out 
HOST = '10.216.1.223'  # this is the hostname for device, to be changed
to read from file when figure that out 
 
##user = raw_input('Username: ')
""" My switch does not require a username """
 
##password = getpass.getpass()
print
loginpassword = getpass.getpass('Login password: ') 
""" The login password and enable password may not be the same """
 
tn = telnetlib.Telnet(HOST) #make less typing for me 
 
##tn.read_until('Username: ') #expected prompt after telnetting to the
router 
##tn.write(user + '\r\n')  #hopefully write username and pass character
return 
##raw_input('ENTER to continue') # just to see if it makes this far 
# this is where the program appears to hang 
 
##tn.read_until('Password: ') #expected prompt after putting in the
username 
tn.read_until('password:')
""" tn.read_until(':') may also work """
 
##tn.write(password + '\r\n') 
tn.write(loginpassword + '\n') 
 
##tn.read_until(HOST + ">") #expected prompt is "hostname>" 
tn.read_until(">")
""" The prompt may not contain the HOST name """
 
tn.write('enable \n') # go to exec mode 
 
##tn.read_until('Password: ') #prompt to go to exec mode 
tn.read_until(':')
 
print
enablepassword = getpass.getpass('Enable password: ') 
##tn.write(password + '\n') 
tn.write(enablepassword + '\n') 
 
##tn.read_until(HOST + '#')  #this should be the prompt after enable
"hostname#" 
tn.read_until(')')  #this should be the prompt after enable "hostname#" 
 
##tn.write('sh int status' '\r\n') #run this command, read this from
file when i figure out how 
tn.write('sh int\n')
""" This command works on a Cisco 4006 with CatOS 6.2 """
 
##tn.read_until(HOST + '#') #prompt once above command has finished
running, useful when reading multiple commands
print
print tn.read_until(')')
 
##tn.write('exit' '\R\N') #disconnect from the session 
tn.write('exit \n')
 
##print tn.read_all() #prints out something, maybe needs to be prior to
"exit" command 
 
##tn.close()
""" I don't think you need this """
 
 

-----Original Message-----
From: tutor-bounces at python.org [mailto:tutor-bounces at python.org] On
Behalf Of STREET Gideon (SPARQ)
Sent: Monday, February 27, 2006 11:36 PM
To: tutor at python.org
Subject: [Tutor] Telnet to cisco device



Hi all, 

I'm trying to get a script together to automate adding a couple of
commands across a lot of cisco switches.  Thought I'd try to get the
script working correctly on one switch first.  I've been reading a few
online tutorials and have managed to kludge up the following (which
fails where commented).  Anyone able to advise where I may be going
wrong?

I'm sitting on a windows box here at work, otherwise I'd see what I
could get expect to do. 

Thanks 

Gideon 

-------------------- 
import getpass 
import sys 
import telnetlib 

HOST = 'switch_name'  # this is the hostname for device, to be changed
to read from file when figure that out 
user = raw_input('Username: ') 
password = getpass.getpass() 

tn = telnetlib.Telnet(HOST) #make less typing for me 

tn.read_until('Username: ') #expected prompt after telnetting to the
router 
tn.write(user + '\r\n')  #hopefully write username and pass character
return 

raw_input('ENTER to continue') # just to see if it makes this far 

# this is where the program appears to hang 

tn.read_until('Password: ') #expected prompt after putting in the
username 
tn.write(password + '\r\n') 


tn.read_until(HOST + ">") #expected prompt is "hostname>" 
tn.write('enable \n') # go to exec mode 

tn.read_until('Password: ') #prompt to go to exec mode 
tn.write(password + '\n') 


tn.read_until(HOST + '#')  #this should be the prompt after enable
"hostname#" 
tn.write('sh int status' '\r\n') #run this command, read this from file
when i figure out how 

tn.read_until(HOST + '#') #prompt once above command has finished
running, useful when reading multiple commands 

tn.write('exit' '\R\N') #disconnect from the session 

print tn.read_all() #prints out something, maybe needs to be prior to
"exit" command 

tn.close() 
  

This e-mail (including any attachments) may contain confidential or
privileged information and is intended for the sole use of the person(s)
to
whom it is addressed. If you are not the intended recipient, or the
person
responsible for delivering this message to the intended recipient,
please
notify the sender of the message or send an e-mail to
mailto:help.desk at ergon.com.au immediately, and delete all copies. Any
unauthorised review, use, alteration, disclosure or distribution of this
e-mail by an unintended recipient is prohibited. Ergon Energy accepts no
responsibility for the content of any e-mail sent by an employee which
is of
a personal nature.

Ergon Energy Corporation Limited ABN 50 087 646 062
Ergon Energy Pty Ltd ABN 66 078 875 902
	

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20060228/14c3b8e7/attachment-0001.htm 


More information about the Tutor mailing list