The networking industry is telling us all that we need to learn Python, we need to do network programming, we need to learn network automation, the CLI is dead! But for most of us nothing has changed and we are thinking what is all the fuss about?
The truth is that using Python for Network Automation is a skill that we all will have to have a basic grasp of soon, so you might as well start leaning some basics.
You don’t have to become a developer to be a network engineer in 2020 but learning a bit of Python will certianly help.
The examples below are short code snippits that you can use to perform some very simple tasks.
All you will need a a device with Python installed and a few test routers to configure.
I will be using EVE-NG running 4 x Cisco CSR1000v routers and Python on Ubuntu 18.04 and using Visual Studio Code to edit my Python code.
Connect to a Cisco Router using Netmiko
The simplest way to connect to a Cisco device using Python is via telnet, but for most environments telnet is disabled, so while it might be good to learn the telnet library I personally think you are best just jumping straight into SSH making it work and then having something that you can use in production.
Netmiko is based on the Paramiko SSH library which is a little bit trickier to get working. Kirk Byers who has written the Netmiko library has made it easier to connect to network devices using Python & SSH. This library works across a broad variety of networking devices, but for the examples below I will be using Cisco
https://pynet.twb-tech.com/blog/automation/netmiko.html
You can either watch the video or follow the code examples below.
# Use Netmiko to connect to Cisco using SSH and run simple commands from netmiko import ConnectHandler CSR = { 'device_type': 'cisco_ios', 'ip': '192.168.1.220', 'username': 'roger', 'password': 'cisco' } net_connect = ConnectHandler(**CSR) output = net_connect.send_command('show ip int brief') print (output)
Simply take this code and save it as something like connect.py
Change the IP address to the IP address of you router and also change the username and password.
Run the Python script and you should see an output of the ip interfaces on your router.
Configure an interface on a Cisco Router using Netmiko
We can take the code above and add some configuration information so that your Python script can actually configure the router.
Add the following code to your connect.py script and you will be able to configure a loopback interface on the router
config_commands = ['interface loopback 2', 'ip address 2.2.2.2 255.255.255.0']
output = net_connect.send_config_set(config_commands)
print (output)
Simply change the interface name and ip address for different configurations.
This is only just scratching the surface of what Python can do, but the main part of learning any new skill is just getting started!
Use ipaddress to generate useable IP addresses
A brilliant built in module to Python is ipaddress.
Full ipaddress documentation is here
https://docs.python.org/3/library/ipaddress.html
With this module you just have to enter a few lines of code and you can start using Python to build out a script that will generate useable ip address for a particular subnet.
Let’s take a simple example. You have been given a subnet and a mask and been asked to generate all the useable IP addresses from within that range.
The subnet is 192.0.2.0/28
You could either do this manually or use a subnet calculator. Or you could use a simple Python script. The benefit of using Python is that once you have generated the first bit, you could easily go on to make that script then configure devices using the addresses that have been generated.
This will not be covered in this post but hopefully you will start to see the power of using Python for Networking.
# Script using ipaddress module to print out useable IP range
import ipaddress
net4 = ipaddress.ip_network('192.0.2.0/28')
for x in net4.hosts():
print(x)
As with the Netmiko example above, save the code as a file called printip.py (do not save the file as ipaddress.py as it will cause issues with the module)
Run the code and you should see the following output.
![python for networking example code](https://www.rogerperkin.co.uk/wp-content/uploads/2020/01/python-for-networking-example-ipaddress-1024x427.jpg)
Simply change the subnet and / or mask in the code to change the output.
This example also makes a great Python interview question for network engineers – so remember this one!
I hope you can see that network programming is a skill that network engineers need to be learning.
Check out my other Python for Network Engineers Examples
or start with my Python Tutorial for Beginners
Python Networking Book
If you want to take this to the next level, I can highly recommend this book by Eric Chou, it’s just been updated to 3rd edition, click the book below to get it from Amazon