What did you Expect? Part 4, Working with Flat Files.

So far so good.  In Part 1 we connected to a Cisco switch and and performed basic Authentication with Expect.  Part 2 we expanded on that and added configuration to our code that added a VLAN and configured an interface.  But as I have already stated we are writing quite a bit of code just to configure a single switch.  So the next step is to add multiple devices and flat files.  I mean yeah we could setup a static list in our code and add our devices to that but why?  Our real goal here is to create functional code that we can use to do real things in real networks.  So that means pulling a list of devices from NMS, IPAM or even our nasty old excel files.  Plus this helps us address  the idea of adding authentication files and other flat file resource pools.  Eventually we will transition the use of flat files into databases so we can do even more cool stuff but we will hold off on that for now.

 

Adding support for flat files:

This is where things get cool and how we can scale a chunk of code to deploy to 10s, 100s, or 1000s of endpoints.  In this first step I modified the code from Part 1 and Part 2 to add the ability to read a file line by line into a variable and create a for loop.  This entire process is accomplished by this one simple cluster of code:

with open('device_file.txt') as fname:
switches = fname.read().splitlines()
for switch in switches:

By applying this before we create our SSH process we called child we are reading the file device_file line by line and placing it in a for loop called switch.  Without changing anything else (other than our STDOUT changes in Part 3), we go from sending config to one device to as many as we want…WOW!  When I started this post I never expected it to be this easy, but it is.

Here is what we will be accomplishing in this script.

  • Create SSH Session to multiple 3560 switches.
  • Authenticate SSH Session
  • Authenticate enable
  • Create VLAN 300
  • Set VLAN 300 on Interface Gigabit 0/10
  • Write changes to memory
  • Exit session

Here is the code.

Here is the device file.

This code is 90% (or so) of what we did in Part 2 , it uses device_file which consists of my lab 3560’s.  If you modify the device file to the IP Addresses of your lab switches you should get the same results.

Scaling Out and Testing:

Now that we are reading a flat file and processing its output into SSH sessions as part of a for loop it is time to scale it out a few more hosts.  To do so you I built out this lab.  In order to make this work with the 3725 router platform that I based the lab on I had to modify the config deployment to something more universal.

Here is what we will be accomplishing in this script.

  • Create SSH Session to multiple 3725 routers.
  • Authenticate SSH Session
  • Authenticate enable
  • Create SNMP Community String pycom that is read only
  • Write changes to memory
  • Exit session

Here is the code.

Here is the device file.

The only changes we made were to the device file name and to config that is delivered.

The cool thing is it just works.  No muss no fuss.  Putting all of these pieces together took some code revamp as seen in Part 3 but now that we have a structure down things are flowing pretty well.  It all becomes about incremental change for now.  In Part 1 we tested our ability to create SSH session to a Cisco IOS device, Authenticate, and then end the SSH session.  In Part 2 we added what we validated in Part 1 and actually pushed configuration in the form of VLAN and interface configuration.  Part 3 was supposed to be about scaling out but instead I had to clean up my mistakes.  Now here we are and we have stacked more functionality on top of the structure.

Just one more thing to add before we call it wraps on Part 4 of our Python/Expect journey.  Like I have stated before we are learning to do this because repetitive tasks suck and kill our productivity.  So every time I would run one of the scripts we are working on I would have to go back to the device it had configured back out the changes manually.  It is annoying on one device but again at any scale it is not viable.  But now I know how to perform magic and by typing one line that can revert as many devices as I need.  So…

Here is what we will be accomplishing in this script.

  • Create SSH Session to multiple 3725 routers.
  • 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.

So thats it!  We have now integrated a flat file in to define the hosts that we are going to run our script against.  So many thing we can do from here.  But instead of getting tricky we are going to break somethings in Part 5.

 

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.