<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=us-ascii">
<TITLE>Message</TITLE>
<META content="MSHTML 6.00.2900.2802" name=GENERATOR></HEAD>
<BODY>
<DIV><FONT face=Arial color=#0000ff size=2></FONT> </DIV>
<DIV><SPAN class=095381708-28022006><FONT face=Arial color=#0000ff size=2>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 +
'#')".</FONT></SPAN></DIV>
<DIV><SPAN class=095381708-28022006><FONT face=Arial color=#0000ff
size=2></FONT></SPAN> </DIV>
<DIV><SPAN class=095381708-28022006><FONT face=Arial color=#0000ff
size=2>----------------------------------------------</FONT></SPAN></DIV>
<DIV><SPAN class=095381708-28022006><FONT face=Arial color=#0000ff size=2>import
getpass <BR>##import sys <BR>import telnetlib </FONT></SPAN></DIV>
<DIV> </DIV>
<DIV><SPAN class=095381708-28022006><FONT face=Arial color=#0000ff size=2>##HOST
= 'switch_name' # this is the hostname for device, to be changed to read
from file when figure that out <BR>HOST = '10.216.1.223' # this is the
hostname for device, to be changed to read from file when figure that out
</FONT></SPAN></DIV>
<DIV> </DIV>
<DIV><SPAN class=095381708-28022006><FONT face=Arial color=#0000ff size=2>##user
= raw_input('Username: ')<BR>""" My switch does not require a username
"""</FONT></SPAN></DIV>
<DIV> </DIV>
<DIV><SPAN class=095381708-28022006><FONT face=Arial color=#0000ff
size=2>##password = getpass.getpass()<BR>print<BR>loginpassword =
getpass.getpass('Login password: ') <BR>""" The login password and enable
password may not be the same """</FONT></SPAN></DIV>
<DIV> </DIV>
<DIV><SPAN class=095381708-28022006><FONT face=Arial color=#0000ff size=2>tn =
telnetlib.Telnet(HOST) #make less typing for me </FONT></SPAN></DIV>
<DIV> </DIV>
<DIV><SPAN class=095381708-28022006><FONT face=Arial color=#0000ff
size=2>##tn.read_until('Username: ') #expected prompt after telnetting to the
router <BR>##tn.write(user + '\r\n') #hopefully write username and pass
character return <BR>##raw_input('ENTER to continue') # just to see if it makes
this far <BR># this is where the program appears to hang </FONT></SPAN></DIV>
<DIV> </DIV>
<DIV><SPAN class=095381708-28022006><FONT face=Arial color=#0000ff
size=2>##tn.read_until('Password: ') #expected prompt after putting in the
username <BR>tn.read_until('password:')<BR>""" tn.read_until(':') may also work
"""</FONT></SPAN></DIV>
<DIV> </DIV>
<DIV><SPAN class=095381708-28022006><FONT face=Arial color=#0000ff
size=2>##tn.write(password + '\r\n') <BR>tn.write(loginpassword + '\n')
</FONT></SPAN></DIV>
<DIV> </DIV>
<DIV><SPAN class=095381708-28022006><FONT face=Arial color=#0000ff
size=2>##tn.read_until(HOST + ">") #expected prompt is "hostname>"
<BR>tn.read_until(">")<BR>""" The prompt may not contain the HOST name
"""</FONT></SPAN></DIV>
<DIV> </DIV>
<DIV><SPAN class=095381708-28022006><FONT face=Arial color=#0000ff
size=2>tn.write('enable \n') # go to exec mode </FONT></SPAN></DIV>
<DIV> </DIV>
<DIV><SPAN class=095381708-28022006><FONT face=Arial color=#0000ff
size=2>##tn.read_until('Password: ') #prompt to go to exec mode
<BR>tn.read_until(':')</FONT></SPAN></DIV>
<DIV> </DIV>
<DIV><SPAN class=095381708-28022006><FONT face=Arial color=#0000ff
size=2>print<BR>enablepassword = getpass.getpass('Enable password: ')
<BR>##tn.write(password + '\n') <BR>tn.write(enablepassword + '\n')
</FONT></SPAN></DIV>
<DIV> </DIV>
<DIV><SPAN class=095381708-28022006><FONT face=Arial color=#0000ff
size=2>##tn.read_until(HOST + '#') #this should be the prompt after enable
"hostname#" <BR>tn.read_until(')') #this should be the prompt after enable
"hostname#" </FONT></SPAN></DIV>
<DIV> </DIV>
<DIV><SPAN class=095381708-28022006><FONT face=Arial color=#0000ff
size=2>##tn.write('sh int status' '\r\n') #run this command, read this from file
when i figure out how <BR>tn.write('sh int\n')<BR>""" This command works on a
Cisco 4006 with CatOS 6.2 """</FONT></SPAN></DIV>
<DIV><FONT face=Arial color=#0000ff size=2></FONT> </DIV>
<DIV><SPAN class=095381708-28022006><FONT face=Arial color=#0000ff
size=2>##tn.read_until(HOST + '#') #prompt once above command has finished
running, useful when reading multiple commands<BR>print<BR>print
tn.read_until(')')</FONT></SPAN></DIV>
<DIV> </DIV>
<DIV><SPAN class=095381708-28022006><FONT face=Arial color=#0000ff
size=2>##tn.write('exit' '\R\N') #disconnect from the session <BR>tn.write('exit
\n')</FONT></SPAN></DIV>
<DIV> </DIV>
<DIV><SPAN class=095381708-28022006><FONT face=Arial color=#0000ff
size=2>##print tn.read_all() #prints out something, maybe needs to be prior to
"exit" command </FONT></SPAN></DIV>
<DIV> </DIV>
<DIV><SPAN class=095381708-28022006><FONT face=Arial color=#0000ff
size=2>##tn.close()<BR>""" I don't think you need this """</FONT></SPAN></DIV>
<DIV> </DIV>
<DIV><SPAN class=095381708-28022006><FONT face=Arial color=#0000ff
size=2></FONT></SPAN> </DIV>
<BLOCKQUOTE style="MARGIN-RIGHT: 0px">
<DIV></DIV>
<DIV class=OutlookMessageHeader lang=en-us dir=ltr align=left><FONT
face=Tahoma size=2>-----Original Message-----<BR><B>From:</B>
tutor-bounces@python.org [mailto:tutor-bounces@python.org] <B>On Behalf Of
</B>STREET Gideon (SPARQ)<BR><B>Sent:</B> Monday, February 27, 2006 11:36
PM<BR><B>To:</B> tutor@python.org<BR><B>Subject:</B> [Tutor] Telnet to cisco
device<BR><BR></FONT></DIV><!-- Converted from text/rtf format -->
<P><FONT face=Arial size=2>Hi all,</FONT> </P>
<P><FONT face=Arial size=2>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?</FONT></P>
<P><FONT face=Arial size=2>I'm sitting on a windows box here at work,
otherwise I'd see what I could get expect to do.</FONT> </P>
<P><FONT face=Arial size=2>Thanks</FONT> <BR><BR><FONT face=Arial
size=2>Gideon</FONT> </P>
<P><FONT face=Arial size=2>--------------------</FONT> <BR><FONT face=Arial
size=2>import getpass</FONT> <BR><FONT face=Arial size=2>import sys</FONT>
<BR><FONT face=Arial size=2>import telnetlib</FONT> </P>
<P><FONT face=Arial size=2>HOST = 'switch_name' # this is the hostname
for device, to be changed to read from file when figure that out</FONT>
<BR><FONT face=Arial size=2>user = raw_input('Username: ')</FONT> <BR><FONT
face=Arial size=2>password = getpass.getpass()</FONT> </P>
<P><FONT face=Arial size=2>tn = telnetlib.Telnet(HOST) #make less typing for
me</FONT> </P>
<P><FONT face=Arial size=2>tn.read_until('Username: ') #expected prompt after
telnetting to the router</FONT> <BR><FONT face=Arial size=2>tn.write(user +
'\r\n') #hopefully write username and pass character return</FONT> </P>
<P><FONT face=Arial size=2>raw_input('ENTER to continue') # just to see if it
makes this far</FONT> </P>
<P><FONT face=Arial size=2># this is where the program appears to hang</FONT>
</P>
<P><FONT face=Arial size=2>tn.read_until('Password: ') #expected prompt after
putting in the username</FONT> <BR><FONT face=Arial size=2>tn.write(password +
'\r\n')</FONT> </P><BR>
<P><FONT face=Arial size=2>tn.read_until(HOST + ">") #expected prompt is
"hostname>"</FONT> <BR><FONT face=Arial size=2>tn.write('enable \n') # go
to exec mode</FONT> </P>
<P><FONT face=Arial size=2>tn.read_until('Password: ') #prompt to go to exec
mode</FONT> <BR><FONT face=Arial size=2>tn.write(password + '\n')</FONT>
</P><BR>
<P><FONT face=Arial size=2>tn.read_until(HOST + '#') #this should be the
prompt after enable "hostname#"</FONT> <BR><FONT face=Arial
size=2>tn.write('sh int status' '\r\n') #run this command, read this from file
when i figure out how</FONT> </P>
<P><FONT face=Arial size=2>tn.read_until(HOST + '#') #prompt once above
command has finished running, useful when reading multiple commands</FONT>
</P>
<P><FONT face=Arial size=2>tn.write('exit' '\R\N') #disconnect from the
session</FONT> </P>
<P><FONT face=Arial size=2>print tn.read_all() #prints out something, maybe
needs to be prior to "exit" command</FONT> </P>
<P><FONT face=Arial size=2>tn.close()</FONT> <BR><FONT face=Arial
size=2> </FONT></P>
<TABLE>
<TBODY>
<TR>
<TD bgColor=#ffffff><FONT color=#000000>This e-mail (including any
attachments) may contain confidential or<BR>privileged information and
is intended for the sole use of the person(s) to<BR>whom it is
addressed. If you are not the intended recipient, or the
person<BR>responsible for delivering this message to the intended
recipient, please<BR>notify the sender of the message or send an e-mail
to<BR>mailto:help.desk@ergon.com.au immediately, and delete all copies.
Any<BR>unauthorised review, use, alteration, disclosure or distribution
of this<BR>e-mail by an unintended recipient is prohibited. Ergon Energy
accepts no<BR>responsibility for the content of any e-mail sent by an
employee which is of<BR>a personal nature.<BR><BR>Ergon Energy
Corporation Limited ABN 50 087 646 062<BR>Ergon Energy Pty Ltd ABN 66
078 875 902<BR></FONT></TD></TR></TBODY></TABLE></BLOCKQUOTE></BODY></HTML>