[Tutor] Print list vs telnetlib.telnet.write differences

STREET Gideon (SPARQ) Gideon.STREET at ergon.com.au
Fri Mar 3 06:41:42 CET 2006


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


More information about the Tutor mailing list