This video covers a basic Netmiko script.
- [Instructor] Here's our first netmiko script. We firstly gonna import netmiko and then we gonna use a dictionary of device information. In this example, I'm gonna configure one switch using SSH. Later on we'll extend and iterate the script to allow us to make changes on multiple switches but again, we gonna start with a simple script and then build on that script. This is once again a Python dictionary.
In the first part of this course, I'm not concentrating on theory. Have a look at the later part of the course where I teach you the theory of lists, tuples or tuples and dictionaries and other theoretical topics in Python. In this initial part of the course, I just wanna get you started automating networks as quickly as possible, so I've created a variable that contains a dictionary of information. We then gonna connect to that switch and send a show command to the switch, in this case, show ip interface brief and then we gonna print out the output.
So, this variable is the output of show ip interface brief and then I print that output. We then create a list of configuration commands. Later on I'll show you how to put your configurations commands into a separate file but initially, we going to start with a list of commands. These are configuration commands and notice in netmiko, we are sending configuration commands as specified here to the switch and then we gonna print the output, so it's as simple as that to view something on the switch and as simple as that to configure something on the switch I've also copied the range that we used previously and here we gonna send this list of config commands to the switch and then print the output.
So, this is very similar to what you saw previously. It's gonna say creating VLAN and a VLAN number. This list of commands is gonna be sent to the switch as a list of configuration commands and then we gonna print the output. So, I'll copy that script, go to the NetworkAutomation container and let's create a file called netmiko1.py.
Make sure you don't use a name netmiko, that won't work. You may wanna use a totally different name but I've called this netmiko1 which will work. Now, in this example, I don't wanna use Python 2.7. I wanna use Python 3. And for that to work, I need to install additional software on the NetworkAutomation container. In other words, if I run python3 netmiko1.py, I get this error, no module named netmiko.
So, to fix that I need to use apt-get update to update references on my NetworkAutomation container and then I'm gonna use apt-get install python3-pip. So, yes to install the additional software, so essentially we just downloading and installing updated versions of software on the NetworkAutomation container.
Once that's installed, we gonna use pip3 install -U or -U and netmiko. Netmiko is now installed. We told that a later release of pip is available. I'm not gonna worry about that. I'm simply gonna use python3 netmiko1.py, in other words, I'm gonna run the netmiko script. There's the output of the show command.
Notice a loop back was configured on the switch and VLANs are configured on the switch. So, on switch one, notice you can see that the switch has been configured by David. Show ip interface brief shows us that a loop back interface has been configured on the switch and show vlan brief will show us VLANs created on the switch.
Now, these VLANs were previously created, netmiko is recreating the VLANs but what I'll do in a moment is remove the loop back and remove the VLAN so that you can see netmiko making the changes. But notice, show ssh shows us that an SSH connection has been made to the switch. Script is now completed, so that's good.
So, what I'll do is say no vlan 2 to 100, no interface loop back zero. So, show ip interface brief. Loop back zero is not configured on the switch. And show vlan brief shows us that only the default VLANs exist in the VLAN database. So, let's run this script again.
Show ip interface brief. Same output is shown on the NetworkAutomation container as we see on the switch. You can see that the loop back has already been configured by the Python script. You can see there that the loop back came up and we can see that VLANs are being created. We already at VLAN seven in the output here.
The script is now creating VLAN 10, so again, show vlan brief, we've got 10 VLANs configured on the switch. Show ip interface brief again. A loop back has been configured onto the switch. So, again this is very similar to what we were doing when we were using telnet but in this example, we using SSH connections.
You can see that the encryption used is aes. Hmac used is sha1, username is David, SSH version is version two. So, again show vlan brief. 20 VLANs have been created on the switch and the loop back has been configured on the switch. Netmiko makes it very simple to configure Cisco devices and other network devices using SSH.
In this example, we've hard coded the username and passwords in our dictionary but you could iterate and improve this script and remove that from your dictionary but this hopefully shows you that using SSH connections with Python isn't difficult.