Lets say you have a couple of Cisco Switches in your environment on which you need to automate some tasks like switching the interfaces such that the device in path gets redirected or if you would like to take backup after running your tests. Manually switching or taking backup of the entire environment filled with catalyst switches and Cisco Routers isn’t fun either. So here is a little experiment using python and pexpect that runs any show command and can also do a backup of a cisco device. There is much improvement to be done and you can tweak this better to run some CLI changes that you are planning. But meh, this is just a starting script and an experiment.
#!/usr/bin/env python #This Script expects you are providing a level 15 user priveledge #Uses FTP & Telnet and is insecure #Will work if run through a linux terminal, not windows #needs ftp server setup and username password added in the cisco device #and is only a sample reference to get started. import pexpect import sys import time import datetime class CiscoSwitch(): def __init__(self, host, username, password): self.username = username self.host = host self.password = password def Login(self): self.child = pexpect.spawn('telnet '+self.host) self.child.expect('Username:') self.child.sendline(self.username) self.child.expect('Password:') self.child.sendline(self.password) self.child.expect('#') self.child.sendline('terminal length 0') self.child.expect('#') return (self.child, self.child.before) def RunShowCmd(self,cmd): self.child.sendline(cmd) self.child.expect('#') return (self.child, self.child.before) def FtpBackupCmd(self,ftpip): self.child.sendline('copy running-config ftp:') self.child.expect(']?') self.child.sendline(ftpip) self.child.expect(']?') DATE = datetime.datetime.now().strftime('%Y_%m_%d_%H_%M_%S') self.child.sendline(DATE+'-'+self.host) self.child.expect('#') return (self.child, self.child.before) if __name__ == '__main__': print 'This program is being run by itself' Switch = CiscoSwitch('184.108.40.206','admin','freebsd') (obj,stdout) = Switch.Login() print stdout (obj,stdout) = Switch.RunShowCmd('show ip int brief') print stdout (obj,stdout) = Switch.FtpBackupCmd('220.127.116.11') print stdout