

Although it too is rather error prone, it’s fairly easy to catch errors as long as you’re expecting (no pun intended) them. However that would require even more time.Įxpect was a good fit in this case. I believe it’s also possible to retrieve the entire configuration from a switch, modify it and put it back. SNMP is one way, but it is complex and prone to errors. There are other ways of configuring Switches too. I could have tried getting Trigger to compile, but I was time constrained so I didn’t bother.

It’s too bad that I couldn’t use any of the existing frameworks. Raise Exception("Unknown switch port '%s'" % (port))Ĭndline('switchport access vlan %s' % (vlan))Įxcept (pexpect.EOF, pexpect.TIMEOUT), e:Įrror("Error while trying to move the vlan on the switch.") Raise OurException("Couldn't log on to the switch")
.png)
The rest of the script is just straight-forward expects and sendline’s. We then simply check that we got the successful one, and otherwise raise an error. The first scenario ‘\(config-if\)#’ is our successful one. Raise OurException("Unknown switch port '%s'" % (port)) How do we detect this? We can tell Expect that we expect two different scenario’s: o = child.expect() If, on the other hand, our port is correct, we’ll simply get a prompt: Switch(config-if)# What if we supply the wrong port? The switch will respond like so: Switch(config)# interface Gi2/0/2 Let’s jump ahead a bit and look at the next interesting problem. We don’t “expect” this, so PExpect will timeout once again while waiting for the “>” prompt.
Reboot cisco switch password#
Otherwise the switch will ask for the password again. If all goes well, we’ll receive the prompt. After the password prompt, we send the password. Raise OurException("Login prompt not received") If we want to catch errors and show the user somewhat helpful error messages, we can use try/except clauses: try: If it does detect the ‘Password:’ prompt, it will then send the switch password and wait until it detects the prompt. It will then raise a pexpect.TIMEOUT exception after 4 seconds. Of course, it won’t get that prompt since the switch is down. If something goes wrong, for instance the switch at 10.0.0.1 is down, expect will wait for 4 seconds, looking for the text ‘Password:’ in SSH’s output. We let Expect know that we expect to see a ‘Password:’ prompt. It tells PExpect to show all the output it’s receiving on our terminal. Then we spawn a new process “ ssh We set the process’ logfile to sys.stdout. This is fairly easy: import pexpectĬhild = pexpect.spawn('ssh % (switch_un, switch_ip))įirst we import the pexpect module. So how do we go about automating this with PExpect?
Reboot cisco switch manual#
This is a simple manual session that changes the Vlan of switch port “ Gi2/0/2” to Vlan 300. Switch(config-if)# switchport access vlan 300 Switch(config-if)# no shutdown Here’s an example session on a Cisco switch we’ll automate with Expect in a bit: $ ssh enableĮnter configuration commands, one per line.

Reboot cisco switch install#
Installation on Debian-derived systems is as easy as “ aptitude install python-pexpect“. There is an excellent Expect library for Python called Pexpect. It’s a wonderful tool, but error handling can be somewhat tricky, as you’ll see further in this article. We can use Expect to scan the output of the program and respond with the username and password when appropriate: spawn ftp host.local It lets the user know this by giving us prompts: $ ftp host.local For example, consider a program that requires the user to enter a username and password. Basically what it does is let the user insert text into the input of the program, and then watches the output of the program for specific occurrences of text, hence the name “Expect”. ExpectĮxpect s a framework to automate interactive applications. Since I was rather time constrained, I decided to fall back to good old Expect. Unfortunately it turns out that the source for sw_script is actually nowhere to be found and Trigger wouldn’t even install properly, giving me a whole plethora of compiler errors. What I found:īoth seemed to be able to get the job done quite well. It’s been a while since I’ve had to do anything even remotely related to switches, so I thought I’d start by googling for some ways to automate tasks on switches. In the spirit of “Automate Everything” I was tasked with scripting some oft needed tasks on Cisco Switches. Scripting a Cisco switch with Python and Expect
