Coding Skill for a Network Engineer

Why a Network Engineer Should Code

Vivekanand Vishwakarma
4 min readMay 14, 2021
Image from cisco.com

When it comes to coding, we network engineers always ask one question, is coding my cup of tea? When you can configure and manage complex routing why not try your hands on coding. What matters is your determination and how excited you are about it. So let’s start coding..

In the era of automation when everything is getting automated, turning blind towards coding is not an option anymore!

Learning coding is not that hard as we think. Just remember “with determination one can achieve anything.”

I am going to talk about why a network/system engineer should learn coding, followed by a small demonstration.

Target Audience: Networking/system folks who are already writing some sort of scripting or willing to learn coding in Python.

Why a network engineer should code

  • You can write better code if you have domain knowledge
  • Everything is getting automated
  • Workloads are moving to Cloud or Hybrid cloud
  • Infrastructure is code in Cloud
  • Most of the product vendors are providing API
  • Using configuration management tools efficiently requires scripting skill
  • Coding is a basic requirement for automation

Some Use Cases for Automation

Automate repetitive task

  • Access-lists(ACL) configuration on Firewall
  • NAT configuration
  • Virtual-server configuration and management on loadbalancer
  • WideIP configuration and management on Global traffic manager
  • SSL Management
  • Audit and compliance
  • DNS record management

Configuration management

  • Device configuration
  • Configuration Backup
  • Configuration restore

Automate troubleshooting

  • Executing multiple “Show commands” at once
  • Automate troubleshooting workflow
  • Correlation of incidents

Automate Compliance

  • Automated configuration check against baseline
  • Automate Audit and compliance workflow

Selecting a right language for coding

You can begin with a scripting language and slowly you can transform yourself to a full stack developer if you wish. A scripting language is often a simpler language, and easier to learn than other languages, but still can do many things.

What is scripting?

  • A script or scripting language is a computer language with a series of commands within a file that is capable of being executed without being compiled. E.g. Python , Shell Script, Perl, PHP
  • Scripting is basically writing code to automate a certain process in a specific environment
  • Scripts are usually short computer programs that do steps that could be done one at a time by a person
  • Scripts automates a job to make it easier and more reliable.

With my personal experience Python is easy to learn but you have choice to learn any language. Below is stats of usage of some of the scripting languages(source: Internet).

Python 37.1%

Bash/Shell scripts 27%

Perl 11.8%

PHP 8.4%

JavaScript 6.7%

Ruby 4.9%

Other 2.1%

Why Python

  • Easy to learn
  • Clear syntax and easy code
  • Built in data structures
  • Python supports modules and packages, which encourages program modularity and code reuse.
  • Python is an interpreted,object-oriented, high-level programming language
  • Since there is no compilation step, its easy to edit,debug and test in runtime and the edit-test-debug cycle is incredibly fast.
  • It offer more than 147,000 libraries

Common Mistakes which happens in beginning

  • Writing code based on assumptions
  • Lack of proper requirement gathering
  • Lack of flowchart/Pseudo code
  • Jumping on Hands-on mode
  • Copying and pasting the code; unsure of what it does
  • Doesn’t follow the best practices
  • Doesn’t know exception error meanings
  • Not meaningful naming the Class/Method/Function such as xyz

How to Start

Learn basic of python, there are plenty for free courses available for beginners . Area of focus for Network Automation in Python could be:

  • Data types in python
  • Variables
  • Expression and operators
  • String and String Slicing
  • List, dict, set
  • Statements
  • Loop and conditions
  • File handling (reading and writing csv files)
  • Paramiko or Netmiko Library for SSH to remote system
  • Ansible

Demonstration

Use-Case: Get BGP status from multiple routers. This might be useful when you are in a troubleshooting call.

Step1: Install python3 on your system

Step2: Install paramiko package

Step3: Define list of device IP address

Step4: Define command to be executed on device (“show bgp vpnv4 unicast all summary” in this case)

Step5: Define paramiko client

Step6: Send commands to device through paramiko

Step7: Parse output as per your requirement.

Sample Code:

# Import Paramiko and other required modules here

import paramiko
import time

command = 'show bgp vpnv4 unicast all summary'


def execute_command(username, password, device_ip):

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(device_ip, port=22, username=username, password=password, timeout=10, look_for_keys=False,
allow_agent=False)

remote_conn = ssh.invoke_shell()
print(f'***SSH session established to the device {device_ip}***')

remote_conn.recv(999) # To remove whatever is currently in the buffer.
remote_conn.send(f'{command}\n')

output = ''
time.sleep(2) # To handle delay in output.
resp = remote_conn.recv(9999) # Buffer size should be adjusted accordingly
output += resp.decode('utf-8') # Output is bytes encoded, you need to decode it

ssh.close() # Close SSH session

return output


if __name__ == '__main__':

username = '' # Your username goes here
password = '' # Your username goes here

device_list = ['10.10.10.1', '10.10.10.2'] # Your list of devices go here. These are dummy IPs for demonstration purpose

for ip_add in device_list: #Iterate over each device IP in list
output = execute_command(username, password, ip_add)
print(output)

This is a simple script to start with. This script will ssh to the device and retrieve a command output. You can add exception handling and other advance python features to it.

Note: Take extra care when you execute configuration changes on device.

Stay happy and blessed!!!

--

--

Vivekanand Vishwakarma

Network, Security, AI/ML and Automation Engineer.CCIE (R&S), AWS Solution Architect and AWS Advance Networking Certified.