##import sys
import telnetlib
HOST = '10.216.1.223' # this is the hostname for device, to be changed to read from file when figure that out
""" My switch does not require a username """
loginpassword = getpass.getpass('Login password: ')
""" The login password and enable password may not be the same """
##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:')
""" tn.read_until(':') may also work """
tn.write(loginpassword + '\n')
tn.read_until(">")
""" The prompt may not contain the HOST name """
tn.read_until(':')
enablepassword = getpass.getpass('Enable password: ')
##tn.write(password + '\n')
tn.write(enablepassword + '\n')
tn.read_until(')') #this should be the prompt after enable "hostname#"
tn.write('sh int\n')
""" This command works on a Cisco 4006 with CatOS 6.2 """
print tn.read_until(')')
tn.write('exit \n')
""" I don't think you need this """
-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of STREET Gideon (SPARQ)
Sent: Monday, February 27, 2006 11:36 PM
To: tutor@python.org
Subject: [Tutor] Telnet to cisco deviceHi 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 telnetlibHOST = '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 returnraw_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 modetn.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 howtn.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:[EMAIL PROTECTED] 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
_______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor