Python Network Automation for beginners

This post is intended for network engineers who wish to use python for network automation for the first time. Moreover, you could have a scenario where devices need to be configured or to collect data from multiple network devices, this would be a boring task of logging into each device and doing it manually. Therefore, this blog – Python network automation for beginners will guide you to set up a portfolio to start network automation.

3 Benefits of Python Network Automation:

  1. Improved work efficiency
  2. Reduced the likelihood of human error
  3. Meeting new Job demands

At this time, here I will explain each step on how a network engineer with no prior knowledge of programming can quickly and easily get started with python network automation.

Prerequisites

A PC with GNS3 and python installed

Steps:

  1. Installation of GNS3
  2. Importing IOS images to GNS3
  3. Setting the loopback interface on PC to access GNS3 network device
  4. Python3 Installation
  5. Mapping loopback adaptor to GNS3 router
  6. Importing netmiko
  7. Connecting python to gns3 router
  8. Running basic codes

Installation of GNS3

  1. Download GNS3 from – https://www.gns3.com/software/download
Python Network Automation using GNS3

Download the appropriate file for your OS

Getting started with Python Network Automation GNS3 Setup

After that, proceed for the Next Step

Python Network Automation programming examples

click on Next

Python network programming

Please note, by this stage, installation is completed, now you can open the GNS3 and name your project.

However, by default, GNS3 doesn’t have any router instance so we have to add the router OS to GNS3.

Getting started with Python Network Automation GNS3 Setup

Importing router image to GNS3 appliance

Following, supported appliance models are listed in GNS3 as above, based on this you can get the images from the respective vendors or you can google and get it.

last but not least, I came across http://tfr.org/cisco which contains a collection of Cisco IOSs. Try this site if it works.

For instance, attaching IOS with the appliance model:

Python Network Programming

For example, select the New Image radio button and then browse the router image on the drive.

Python Networking Projects

After that, click Next

Getting started with Python Network Automation GNS3 Router

select the proper platform for the image.

python network automation course

After clicking Next, here we have completed the appliance setup process.

Moreover, newly added c7200 can found in the list of routers, drag this device into the workspace and then run it, you can get the console window from its menu

Please note that now we have the device running in GNS3 but we won’t be able to reach this device from our PC, so we must create a loopback interface on PC and make connectivity to the GNS3 router.

Configuring a Loopback interface on PC to access GNS3 router

open run and type hdwwiz

python network automation framework

Proceed with OK

python network automation training

Following, click next

python network automation book
python network automation pdf

Select Network adapters

python network automation libraries

Furthermore, select Microsoft from the left window and then select Microsoft KM-TEST Loopback Adapter.

practical python cisco network automation

Click Finish

network automation python Cisco

Indeed, now you can see the loopback network adaptor in windows network connections. furthermore, configure IP for the loopback network adaptor so that the GNS3 network device can connect to it.

Now we will pull this loopback network adaptor inside GNS3 and will configure a connection to it.

network automation with python for network engineers

Drag and drop the “cloud” instance from “all devices” category to the workspace

python network automation projects

Moreover, right-click on the cloud instance and select configure – you will find the list of interfaces under the ethernet interfaces tab, here you can select the loopback interface that we have configured just before.

Getting started with Python Network Automation Cisco
Python network Automation beginners setup

Now make a connection to each other using the link tool. Please note, here the router interface indicating red because the interface is in a shutdown state.

I have renamed the cloud instance and labeled the IP address of the interfaces for your understanding.

Getting started with Python Network Automation windows

Configure the loopback interface IP address.

Verify you have network reachability to the router

Getting started with Python Network Automation

For instance, here, in my router, I have configured SSH to access the router.

Reference – http://www.mustbegeek.com/enable-ssh-in-cisco-ios-router/

Download python from – https://www.python.org/downloads/

Python installation is straight forward. Make sure you set the environment variable for python, you can verify this by simply typing python in the windows command line.

Getting started with Python Network Automation windows

Change the present working directory of windows commands line to Scripts folder of python installed files.

change command-line directory by the cd command.

cd <your directory>

Install Netmiko module using the PIP tool.

netmiko is a Python library which makes the connection to network devices via SSH.

netmiko installation command – pip install netmiko

Note: make sure you have internet connectivity

python network automation examples

Now you can import Netmiko to your python program.

Python Network Automation first program

Here is the first program

from netmiko import ConnectHandler # Module which enables SSH connection
platform = ‘cisco_ios’
host = input(‘Enter the HostName or IP Address: ‘)
username = input(‘Enter the Login UserName: ‘) # edit to reflect
password = input(‘Enter the password: ‘) # edit to reflect
device = ConnectHandler(device_type=platform, ip=host, username=username, password=password)
output = device.send_command(‘show running-config’)
print(output)
input()

python network automation examples

Program output – showing running configuration

Summary

In conclusion, this is a basic program, here we just got familiarized to get connected to a network device using python. We will cover more interesting automation codes in future posts.

For further reading, please visit the blog post Cisco new certification track to know more about newly published DevNet certification programs from Cisco. Happy learning

7 Comments

Add a Comment

Your email address will not be published. Required fields are marked *