What did you Expect? Part 5, Basic Error Handling.

In the first four parts of What did you Expect, we covered the basics of getting started with automating interactions for network equipment.  In the first few posts it was important have a networking environment that  was 100% stable.  The last thing I needed when I was trying to learn to use python to automate network devices that were randomly unresponsive and would crash my code.  In order to accomplish that I built a test network you can read about here in GNS3, created a basic configuration to enable a IOS device to be remotely managed.  I also wrote a quick multi-device ping tool to verify that all the devices are responsive before we run remote code against them.  I made my life easy.  But as all operators know our lives are not that cut and dry.  So I started to break things…and my code did not like me.

As the code was in post 4 if it would attempt to connect to a device that offline or rejecting connections it would eventually timeout and the code would crash.  Because in real life our code will inevitably hit non-responsive equipment this had to be fixed.  So this entire post focuses on how to do just that.

Adding Basic Error Handling:

Falling back on my code mentor Tyler we worked through what I needed to happen in my code.  The long and short of it was that while the code was moving through the for loop, if it encountered a device in the list that was not responding it would simply ignore it and move on.  Getting to the point that you know what you have to accomplish is a key step to moving forward.  To accomplish this we had to modify the first expect statement.


What we modify with is an try/except statement.  This statement is Labeled with our expected outcome which is we get the prompt for Password.  If we don’t get the Password prompt then it drops to the default TIMEOUT for pexpect which is 30 seconds.  At timeout we then print Connection to IP of Switch failed and then use the python command continue to move on to the next device in the device file.

This snippet of code shows our syntax.

got_prompt = True
except pexpect.TIMEOUT:
got_prompt = False
if not got_prompt:
print "Connection to %s failed!" % (switch)

Once we have modified the code it is time to test it out.

Here is what we will be accomplishing in this script.

  • Create SSH Session to multiple 3725 routers.  One router will be disabled.
  • Application will notify and bypass non-responsive router
  • Authenticate SSH Session
  • Authenticate enable
  • Delete SNMP Community String pycom that is read only
  • Write changes to memory
  • Exit session

Here is the Code.

Here is the Device File.

To validate I used the GNS3 lab from this post and I disabled FE 0/0 to created a non-responsive router to validate the code.

There you go we now have error handling.  Now we can move on to cleaning up the rest of the code using what we have learned about try/except statements and then move on to basic database integration.  Stay tuned lots more to come!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.