Table of Contents

This Python Doesn’t Bite!

It is nearly going to be 3rd week since I started to learn python. I would like to declare that it is as it claims – “easy to read and to write”. I would like to thank Guido van Rossum, the person behind creating this seemingly cool but powerful programming language. As a network engineer, I didn’t need much of programming skills for last 11 years, but as you might have read or watched in multiple articles, whitepapers or videos, times are changing fast. Automation is here for quite a long time, it was considered more of a plus skill, something more like a luxury or icing on the cake, but it is soon becoming necessity as software defined networks and virtuization technologies are taking over traditional networks.

Why Network Engineers Need to Learn Python?

Those were the years of CLI or GUI based configuration, coming are the days of API based configuration. I am still learning the use cases, what programming can do for the network engineers, first and foremost the direct uses in networking are:

  • Automating configuration tasks or even troubleshooting network issues
  • Mass configurational changes on Cisco IOS-like devices
  • Interacting with API of modern platforms (VMware, Cisco APIC, F5, Arista, etc)
  • Using power automation libraries such as NAPALM, Netmiko, Exscript

These are handful uses I can write without needing to think much or doing any research before writing. Although all this seems promising, but how about reaching to the point where you can start to use python for something useful? From my experience, I can see that learning gradually, with 3-4 hours per week effort you could start to code easily. First things first, no need to hurry, you will not become “network programmer – the awesome” in first week or even 3. Also, good to keep in mind that programmers, who wrote automation libraries and python modules had years of coding and networking experience behind them. As it is coming age of network programming and at present a network professional has nothing to worry in terms of being not able to program, but as age-old Tortoise and Hare story teaches, it is always good to start now and work towards goal slowly and consistently.

Where to Start From?

I started learning python with courses on coursera, the series I am attending, is named “Python for Everybody”, it is specialization course with 5 parts. With Dr. Chuck aka Dr. Charles Severance from University of Michigan, leading this course, it has been a great learning experience starting from “Hello, World!” to writing codes for parsing XML, JSON and interacting with databases. There are few other sites such as http://www.practicepython.org/, http://www.w3resource.com/python-exercises/ listing python exercises with solutions, a great resource for beginners. I would like to share a simple code here, which I can write independently after just 2 weeks after starting my python journey:

 

name = raw_input(“Enter your name: “)

age = int(raw_input(“Enter your age: “))

mprint = int(raw_input(“How many times do you want to print the mssg? “))

 

def agecal(name, age, mprint):     //start of method / function

     if age < 1 or age >= 100 or age == 0 or mprint <= 0:

          if age == 100: print “Congratulations,”, name, “you are a living wonder!”

     exit()

     print “please enter age 1 or more”

     age = int(raw_input(“Enter your age: “))

     if mprint <= 0: print “please enter multiprint number more than 0”

          mprint = int(raw_input(“How many times do you want to print the mssg? “))

     century = 100 – age

     while mprint >= 0:

           print “Hi”,name + “, you will turn 100 in”, century, “years” “\n”

    mprint = mprint – 1

return;   // end of method / function

 

agecal(name, age, mprint)

 

What Does Above Code Do?

This simple code asks user to input values for 3 variables – name, age, mprint and function agecal(x,y,z) accepts those values as arguments and prints number of line to user with name and years they have to reach 100 years of age. There are some checks in the code to check if user inputs correct values (such as age or mprint more than 0). The function agecal() is completely unnecessary in this code, it was my attempt to put all logic flow into function and then call this function whenever I need to print information to user. Variable “mprint” simply asks user how many times they would like statement to be printed. There is still some space for improvement, such as using error handling in case user inputs text while inputting age or mprint, or inputs integer in place of name, although different case when user likes to be called by number rather than a name!

From Simple Codes to Network Automation

Once you feel ready enough to write simple code in python, or expressing your logic in language spoken by pythonists (python programmers are also known as Pythonista), it will be good time to go a little further and get yourself familiar with Python object oriented programming. It is not necessary but it will help in future to read those complex python libraries and modules and understand the program flow. Next step after this will be to check Netmiko (https://github.com/ktbyers/netmiko) python library, it is an awesome python module which lets you interact with number of leading networking vendor (Cisco, Juniper, F5, Vyatta, etc) devices. It could be checked on the above URL which platforms are currently supported by Netmiko as the list keeps getting updated. Next pit stop after that should be getting familiar with Ansible and another python library called NAPALM (https://github.com/napalm-automation/napalm).

In next python article, I am planning to write a code which can do something interesting with on Cisco IOS based devices like – sorting useful info from output or configuring routing protocols.

Essential Skills For A Successful Network Engineer

Upcoming Webinars

© Copyright 2013 - 2020 Testclue, All Rights Reserved, Terms and Conditions of use Privacy Policy Could be found here. Refund Policy is available here. Disclaimer : F5, Cisco, VMware, pfsense, VyOS are trademarks and logos of respective companies  holding them, Testclue is not official partner or provider of vendor specific courses and material.