Help please using telnetlib module

dudds dudds at netspace.net.au
Sat Dec 23 20:51:31 EST 2006


Hi Guys,

I just started learning Python a couple of days ago and to put some of
what I learnt into practice. As such I thought I might try and write a
simple program (based on examples I had seen) that would allow me to
log into a Cisco router, enter configuration mode, change an interface
description and also grab a snapshot of the running configuration.

So far I've managed to be able to log in and change the interface
configuration. The tn.read_until() function seems to work pretty much
as I expected however for the life of me I cannot get the running
configuration to display with any of the read functions.

Suggestion please........my code thus far follows:
import getpass
import sys
import telnetlib

#This can actually log in and change an interface description on a
router


HOST = "xxx.xxx.xxx.xxx"
#user = raw_input("Enter your remote account: ")
#password = getpass.getpass()
password = "tacis345int"

tn = telnetlib.Telnet(HOST)
#tn.interact()
tn.read_until("Password:")
tn.write(password + "\n")
#if password:
#    tn.read_until("Password: ")
#    tn.write(password + "\n")
tn.read_until("port>")
#tn.write("show version\n")
#tn.write("\n\n")
#tn.read_until("port>")
tn.write("enable\n")
tn.read_until("assword: ")
tn.write("tacis345int\n")
tn.read_until("port#")
tn.write("conf t\n")
tn.read_until("config)#")
tn.write("int ethernet0\n")
tn.read_until("config")
tn.write("desc This was written by my script\n")
tn.write("show run\n")
print tn.read_very_lazy()
tn.write("exit\n")
tn.read_until("#")
tn.write("quit\n")
tn.close()
print tn.read_all()


The only output I get from this is:
-if)#
show

I have tried the other read options but nothing seems to work for me.
I'm obviously missing something, so if anyone could shed some light
that would be great.




More information about the Python-list mailing list