[Tutor] Print list vs telnetlib.telnet.write differences
David Heiser
David.Heiser at intelliden.com
Fri Mar 3 07:24:57 CET 2006
I believe you can submit the new config content as a blob where blob =
string.join(lines). It looks like your "switch" uses IOS, not CatOS, so
make sure you send "config t" first.
And I would strip out the \r's.
Then maybe:
------------------------
tn.write("\03") # Assures the device is in enable mode
x = tn.read_until("#") # The "x =" seems to help flush the read
buffer
tn.write("conf t\n")
x = tn.read_until("#")
tn.write(blob)
-----Original Message-----
From: tutor-bounces at python.org [mailto:tutor-bounces at python.org] On
Behalf Of STREET Gideon (SPARQ)
Sent: Thursday, March 02, 2006 10:42 PM
To: tutor at python.org
Subject: [Tutor] Print list vs telnetlib.telnet.write differences
Hi all,
I've got the following snippet in a script I'm playing around with and
need some help.
Basically the script telnets to a cisco switch, "tn.read_until" a
subsection of the config, outputs to a file, then using readlines brings
it back into the script as a list where I'm changing some fields and
then outputting it again. (the script is long and messy but it's my
second attempt at writing something to make my network admin life
easier, I'm sure over time I can make it a bit more robust).
Anyway, "lines" is the list read from a text file, and a "print lines"
looks like:
['banner exec ^\r\n',
'##################################################\r\n',
' EMKYSW34 - User Access Switch, 2nd Floor\r\n', " Corner of XXXXXX
and XXXX
St's XXXXXX XXX\r\n", ' Site Contact: KXXX SXXXXX XX XXXXXXX0\r\n',
'
System Contact: XXXXX Helpdesk XXXXXXXXXX\r\n',
'############################### ###################\r\n', '^\r\n']
As I'm trying to get the above cleaned up banner sent to the telnet
session I use the following command (without the print x # part).
tn.read_until('#')
for item in lines:
x = item
print x #tn.write(x)
The problem I'm stumbling over is that when I print x, the output is
what I want. If I delete the print x and #, leaving only tn.write(x) on
the last line everything looks good except it writes the 1st item in
"lines" (the banner exec command) twice, once at the beginning where
it's supposed to, but then once again at the end. See except below for
example.
Anyone able to explain why that is happening or is it me just not
understanding what I'm doing? Hope my explanation is clear I'm still
unfamiliar with the exact phrasology.
<SNIP>
emkysw34#conf t
conf t
Enter configuration commands, one per line. End with CNTL/Z.
emkysw34(config)#banner exec ^
##################################################
EMKYSW34 - User Access Switch, 2nd Floor
Corner of XXXXXX and XXXXXX St's XXXXXX XXX
Site Contact: XXXX XXXXXX XX XXXXXXXX
System Contact: XXXXX Helpdesk XXXXXXXX
##################################################
^
banner exec ^ <----heres the second instance being printed for no
reason I can understand Enter TEXT message. End with the character '^'.
####exit ############## <SNIP>
Thanks
Gideon
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
_______________________________________________
Tutor maillist - Tutor at python.org
http://mail.python.org/mailman/listinfo/tutor
More information about the Tutor
mailing list