Help please using telnetlib module

D

dudds

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.
 
B

Birdman

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('#')
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top