Take your network programming skills to the next level. Learn how to iterate and scale scripts with Netmiko—a Python library for configuring SSH connections to routers and switches.
- [Instructor] Iterate, in other words, start with some code and then improve it, and then improve it again, and again, and again. Constantly improve your code to accomplish more. Don't try and do everything in one go. As the saying goes, again, Rome wasn't built in a day. Babies don't run marathons. They turn over, later on they crawl, then they walk, then they run, as they grow up and become more confident.
So the same here, start small and then grow your scripts or iterate your scripts. In the first script we're gonna connect to a single device. 192.168.122.72, and run the command show IP interface brief. That device is this first switch in the topology. So we're gonna get to this Ubuntu host to SSH, to the switch, and return show IP interface brief. Then what we're gonna do is iterate and change the script to use a file of commands.
So we'll open up a file called commands_file, read the commands in the file, and then send those commands to the switch. In both of these scripts we are connecting using Netmiko to the switch and then doing something. In the first script, we run a single command, show IP interface brief. In the second script, we can send multiple commands to the switch based on this file called commands_file.
You could use any file name here. That's simply the file name that I've used. I've also got a list here, rather than pointing to a single device, I've got a loop consisting of IOS devices. Now you could add additional devices here by adding them to this list. But rather than doing that, we're gonna iterate and use a file with device IP addresses. So we've got a for loop, which allows us to connect to multiple devices as specified in the devices file.
I'll talk about each of these scripts in more detail in the subsequent videos. This is just gonna be an overview of the various scripts. In script four, we're gonna prompt for the username and password. So rather than embedding the username and password in the script, we're gonna prompt the user to provide that information. In script five, we're gonna implement some error handling. What happens if the device is not available? So what happens if we have a timeout to the device? What happens if there's an authentication failure? Or another issue where SSH is not enabled on the switch? Or the VTY line doesn't permit SSH? So rather than the script breaking, what we're gonna do here is use exceptions, which in Python allows us to capture errors.
So we're gonna try to connect to the device, and based on an exception or error we're gonna do something. If there are no errors, we're going to connect to the device and run a list of commands. And that'll be done for multiple devices as specified in the devices file, and we'll run multiple commands as specified in the commands file. What we'll do then is iterate again and have multiple command files. One for a switch and one for a router in this example.
So when we connect to the device, we'll firstly do some error checking, and then what we'll do is look at a list of devices. See I've got a few devices such as VIOS layer two advanced enterprise. That's a viral IOS layer two switch. Here I've got a viral IOSV router. Those two options account for these switches and routers in this topology. But in addition, I've got a Cisco 1900 router and 3750 router.
Those are physical devices that I'm connecting to through my Nat cloud. Those are physical devices in my local network. So based on the device type, what we could do then, is run different commands. In this first version of the script, I'm only looking for IOSV layer two switches, and IOSV routers, and then running a list of commands, depending on which device it is, whether it's a router or a switch.
And then I'll iterate again, and run different commands on a 1900 as well as a 3750 switch. So we've iterated from 14 lines of code to 74 lines of code, which will then extend further. So the moral of the story, you start with a small piece of code, get something working, and then try and add an additional feature to your code. Get that working, and then add a third feature to your code.
And just iterate, over and over again, until you get to the point that you're happy with your code. Don't forget to add comments to your code, so that you know what the code is doing. Otherwise, six months from today, you may not know what your code actually does. So this code could be enhanced even more with better commenting. The moral of the story is start small and then iterate or improve your code.