I found this useful bit of Python code for implementing scriptable telnet sessions. The above linked version of code seems to have been written in 2002 and appears to have a few problems with Python v2.5.x. I’ve made a few modifications and you can download my version here. Tim Keating is the original author of the script.
I use this script to do some administration work on Cisco routers. I’m sure there are a couple of nifty things you could do with it, such as make a script to download and backup all your router configs.
You script your telnet sessions like this:
script =
“”"ru Username:
w user
ru Password:
w yourpass
ru ROUTER#
w terminal length 0
ru ROUTER#
w show config
ru ROUTER#
cl
“”"
Execute the script like this:
con = telnetscript.telnetscript(‘yourserver.com’)
self.config = con.RunScript(self.script.split(‘\n’))
self.config = self.config.split(‘\n’)
Results will be stored in the above example as an array of strings in self.config.
Related posts: