[Tutor] Telnet to cisco device

STREET Gideon (SPARQ) Gideon.STREET at ergon.com.au
Tue Feb 28 07:36:05 CET 2006


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/7d5481b0/attachment.html 


More information about the Tutor mailing list