OK let me give you a brief break-down. For many networking devices used now, you configure it through a CLI or command-line interface. This can be fine when you have a small office with maybe 3 switches but it can get very tedious and extremely time-consuming when you're on a larger scale, say 100 switches. How do you cut this down? Network automation. You use software to streamline configuration, be it a python script or your SDN controller (that one for another blog post). OK cool, now we're on the same page let me show you something cool I did
Setup, setup, setup
Ok quickie, to get started you can go to the following links:
- https://github.com/ktbyers/netmiko (github page with instructions on how to install)
- https://pynet.twb-tech.com/blog/automation/netmiko.html (page giving some solid examples on its use )
Nice, now let's look at what I did
What did you use?
I ran up a GNS3 environment (GNS3 is a network emulation tool that allows you to use real Cisco devices, but virtually) and connected it to my home network with the Cloud module.
I ran the Python script from an Ubuntu VM in VMware Workstation Player.
The Python library, netmiko, allows you to SSH into all your devices and send commands from there and that's exactly what I did.
from netmiko import ConnectHandler host = "192.168.0.50" username = 'jordan' password = 'password' device_type = 'cisco_ios' ssh_connect = ConnectHandler(ip = host, username=username, device_type = device_type, password=password) vlan_commands = ["vtp domain jordan.local", "vlan 10", "name Management", "vlan 20", "name Tech", "vlan 15", "name Sales", "vlan 40", "name CSR"] ssh_connect.send_config_set(vlan_commands) vlans = ["1", "15", "10", "20", "40"] for vlan in vlans: stp = ["spanning-tree vlan %s priority 4096" % vlan] ssh_connect.send_config_set(stp) hosts = ["192.168.0.50", "192.168.0.51", "192.168.0.52"] for host in hosts: ssh_connect = ConnectHandler(ip = host, username=username, device_type = device_type, password=password) vtp_state = ssh_connect.send_command("sh vtp status") print "\n\nCURRENT HOST: %s" % host print vtp_state vlans = ssh_connect.send_command("sh vlan br") print vlans stp_state = ssh_connect.send_command("sh spanning-tree") print stp_state
OK I'll be honest, I didn't make much documentation for this one. BUT it's really simple to understand if you have an OK understanding of IOS and have looked at Python before so I'll let myself slide this time. Essentially I made the script make the first host a VTP Server and the STP root bridge and displayed the respective STP and VLAN outputs when I did that from the other switches. Pretty basic!
Really short and sweet post today and I won't say that's because all I had to say was this buttt I've been a little busy recently so this is all I really had the time to muster. I hope you learned something new today and I hope to see you in the next post
One thought on “The Really Cool Thing About Network Automation (using Netmiko)”
That’s super helpful!