By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,907 Members | 2,031 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,907 IT Pros & Developers. It's quick & easy.

Help please using telnetlib module

P: n/a
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.

Dec 24 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Simplest explanation is that you can't do a 'show run' from global
configuration mode....

try something like

#exit global configuration mode
tn.write('end\n')
print tn.read_until('#')

#disable pause after 24 lines
tn.write('term len 0\n')
tn.read_until('#')

#now show the entire running-config
tn.write('show run\n')
print tn.read_until('#')

Dec 24 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.