MPLS Automation Using Netmiko

Due to Computer Networking technology are shifting to software-based recently, we are expected to have capability in programming language as part of Network Engineer skill set today. It doesn’t mean to become a Network Engineer you have to be a Programmer too, it doesn’t mean Programmer jobs will be taken over by Network Engineer or vice versa. However, being Network Engineer with programming knowledge will be an advantage. You can automate network configuration using a codes/script to ease your daily operation, eventually the efficiency is the main objective.

I have a limited experience with Java when I was studying at my engineering college few years ago (in fact, I have no background study in IT). I believe, as an IT engineer at least you have an experience in programming too. I prefer to study Python programming now as its easiness to learn and has so many module that you can use, moreover it has large community outside there.

In this session, I will share my codes to automate MPLS L3VPN configuration using Python. Thanks for Kirk Byers who has developed Netmiko library for Python. Alright! As usual, here the network topology as your reference:

TOPOLOGY

Well, you may see there’s no point to automate this kind of simple topology using a Python code. Even somebody with expert level can finish it using manual CLI command easily and faster. But my objective here is just want to show how to use Python-Netmiko. So that, it can help you in daily operation to push any template configuration OR it might be helpful in medium-large scale topology.

As you can see in the topology above, I used GNS3 to simulate Cisco IOS that was connected to virtual-machine miniserver-002. There’s a pre-configuration on each router to enable remote management (dotted line). The pre-configuration are consist of:

  • IP address for remote management. For simple setup, I configured the remote management segment in the same subnet (10.10.10.x/24) with miniserver-002. Make sure all remote management interface are reachable from miniserver-002.
  • SSH v2 on each router.
  • The rest, let’s do it in Python codes… 😉
CONFIGURATION

I created a script using Python 2.7 due to its stability and compatibility with Netmiko:

"""
This code is written to automate Basic MPLS L3 VPN Configuration on Cisco Router.
Netmiko module is utilized to run Cisco command via SSH remotely.

Coded by:
@rpoernama
"""
from netmiko import ConnectHandler
from datetime import datetime
import sys


#List of Devices
P1_rtr = {'device_type':'cisco_ios', 'ip':'10.10.10.11', 'username':'cisco', 'password':'ciscopass'}
P2_rtr = {'device_type':'cisco_ios', 'ip':'10.10.10.12', 'username':'cisco', 'password':'ciscopass'}
PE1_rtr = {'device_type':'cisco_ios', 'ip':'10.10.10.13', 'username':'cisco', 'password':'ciscopass'}
PE2_rtr = {'device_type':'cisco_ios', 'ip':'10.10.10.14', 'username':'cisco', 'password':'ciscopass'}
CE1_A_rtr = {'device_type':'cisco_ios', 'ip':'10.10.10.15', 'username':'cisco', 'password':'ciscopass'}
CE2_A_rtr = {'device_type':'cisco_ios', 'ip':'10.10.10.16', 'username':'cisco', 'password':'ciscopass'}

#File config name
cfgFiles = ['p1.cfg', 'p2.cfg', 'pe1.cfg', 'pe2.cfg', 'ce1_a.cfg', 'ce2_a.cfg']

allDevices = [P1_rtr, P2_rtr, PE1_rtr, PE2_rtr, CE1_A_rtr, CE2_A_rtr]

#Function to push config file into the devices
def setConfig():
        startTime = datetime.now()

        print("Pushing configuration to the devices, please wait...")
        for i in range(len(allDevices)):
                netconnect = ConnectHandler(**(allDevices[i]))
                cmd = netconnect.send_config_from_file(cfgFiles[i])

        endTime = datetime.now()
        totalTime = endTime - startTime
        print("Finish!!! Total time: %s" %totalTime)

        #Closing the connection
        netconnect.disconnect()
        print("Connection has been closed")

def pingPair():
        rConnect = ConnectHandler(**CE1_A_rtr)
        rOutput = rConnect.send_command("ping 192.168.12.1 source 192.168.11.1")

        print("Ping from CE1_A LAN to CE1_B LAN")
        print("================================")
        print(rOutput)

        #Closing the connection
        rConnect.disconnect()
        print("Connection to the device has been closed")


print"Welcome at Auto-MPLS Configurator, please select your option"

while True:
        print("1] Configure MPLS L3 VPN to All Devices")
        print("2] End-to-End Connection Testing")
        print("3] Exit Program")

        inputOption = raw_input("Please input the number option: ")

        if inputOption == "1":
                setConfig()
        elif inputOption == "2":
                pingPair()
        elif inputOption == "3":
                sys.exit(0)
        else:
                print("Sorry, your option is still not available!")
                sys.exit(0)

Then I save it as auto-mpls.py file in the same folder where all template config files are stored. Again, please make sure the server can connect using SSH to all remote routers. This script will push the template configs sequentially start from P1, P2, PE1, PE2, and so on. You can execute this script using Python and wait until it finished the configuration, here’s the capture when I was running the code:

It takes about 2-3 minutes to push all configurations. It depends on the server performance, communication speed, and the template files size. As we can see in the capture below, when the configurations already pushed on each router it will show you the status of BGP, MPLS, etc. between each router:

P1

P2

PE1

PE1

CE1-A

CE1-B

Next, we need to verify whether the MPLS link is established by executing command ping from CE1-A to CE1-B. This is the easiest way to perform end-to-end testing. Of course, we can specify another commands to verify the MPLS link status. You can specify it using send_config_set(arguments) function in Netmiko. For this simple testing, I choose option 2 then it will execute ping command in background and it will show you the output result:

SUMMARY

Netmiko is a Python module which can gives a benefit for NetEng nowadays. It can helpful for doing daily (repetitive) jobs easily. This module also supports many platforms including:

  • Cisco IOS
  • Cisco IOS-XE
  • Cisco ASA
  • Cisco NX-OS
  • Cisco IOS-XR
  • Cisco WLC (limited testing)
  • Arista vEOS
  • HP ProCurve
  • HP Comware (limited testing)
  • Juniper Junos
  • Brocade VDX (limited testing)
  • F5 LTM (experimental)
  • Huawei (limited testing)
  • A10 (limited testing)
  • Avaya ERS (limited testing)
  • Avaya VSP (limited testing)
  • Dell-Force10 DNOS9 (limited testing)
  • OVS (experimental)
  • Enterasys (experimental)
  • Extreme (experimental)
  • Fortinet (experimental)
  • Alcatel-Lucent SR-OS (experimental)

Or even you can develop your own vendor, by following this tutorial from Kirk Byers:
https://github.com/ktbyers/netmiko/blob/develop/VENDOR.md

Last but not least, happy coding and enjoy your coffee!

Reference:

Netmiko Quickstart – networkcode
Netmiko Library – Kirk Byers
BGP Automation Using Python – Kirk Byers

Advertisements