• Skip to main content
  • Skip to header right navigation
  • Skip to site footer

Roger Perkin

Network Automation Architect

  • Network Automation
    • Network Automation Courses
    • What is NetDevOps?
    • Workflow Orchestration
    • Ansible Automation Platform
    • Ansible Workshop
    • What is Network Automation?
    • Network Automation Tools
    • ContainerLab
    • Ansible Training
      • What is Ansible?
      • Ansible Tutorial for Beginners
      • Ansible Network Automation
      • Ansible Inventory Example
    • Python Network Automation
      • Nornir
      • Python Network Automation Course
      • Python for Network Engineers
      • Python VENV / Virtual Environment Tutorial
      • Python Tutorial for Beginners
      • pyATS
    • Network Source of Truth
      • NetBox
      • Infrahub
    • NetDevops
    • DevOps Tutorial
      • Git Training
      • Terraform Training
      • Linux Training
      • Kubernetes Training
      • Devops Training Course
      • Azure Devops Training
    • Terraform
    • GIT
      • Git Commands
      • What is GitHub?
    • Docker Training
    • Confluence
    • Microsoft Azure
  • Cisco
    • ISE
    • SD WAN Training
    • Password Recovery
    • Software-Upgrade-Guides
    • BGP
    • Data Center
    • WIRELESS
  • CCIE
  • Blog
  • About
    • My Red Special Guitar
  • Contact

Start using Python for Networking today

Home » Network Automation » Python Network Automation

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

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

Category: Python Network Automation
ansible course for network engineers
Get Access to my Ansible Course NOW
Previous Post:How to clean laptop screen and keyboard
Next Post:Ansible vs Python for Network Automationansible vs python featured image

Sidebar

Hi I'm Roger Perkin,
Based in the UK working as a Network Automation Architect, CCIE #50038
About Roger | Twitter | Linkedin

python course for network engineers

More Python

  • Python Network Automation
  • Cisco pyATS Tutorial for Beginners
  • Network Automation Certification
  • Network Automation with Python
  • Python VENV / Virtual Environment Tutorial

Topics

Network Automation
Ansible
Python for Network Automation
CCIE
Cisco ISE
F5 Certification
BGP
OSPF
Network Automation Conferences
auvik promo banner
Pluralsight Trial

Git for Network Engineers

Ansible vs Nornir

Start learning today with my Network Automation Courses

Master Ansible, Python, Git, Nornir, Jenkins and more..


Buy me a coffeeBuy me a coffee

ansible network automation course

Have you seen my YouTube Channel?

YouTube Subscribe

Let’s get started

Take a look at my premium courses on Ansible, Nornir & Git or buy them all with the Network Automation Bundle!

Network Automation Courses

Navigation

Python VENV Tutorial
Python for Network Engineers

Network Automation
Network Automation Courses
Network Discovery Tools
Network Automation Conferences
Ansible Training
What is Ansible?
Devops Tutorial
Network Source of Truth
DevOps Glossary
Network Monitoring Software

Contact

Contact

Get in touch with me here

[email protected]

  • Twitter
  • LinkedIn
  • YouTube
Buy me a coffeeBuy me a coffee

Copyright © 2025 · Roger Perkin · All Rights Reserved · Privacy Policy – Terms