Learning Python for Networking

Learning Python for Networking

Python the Hardway – https://learnpythonthehardway.org/book/?__s=ng1wguqfgy4mttisodvu

Python2 vs Python3 (relevant differences for beginners):

Darren O’Connor has a recent blog post about passing arguments into a Python script (Darren is a dual-CCIE, JNCIE who has some blog posts about Python):

For more complicated argument parsing check out Argparse (requires Python 2.7) or getopt:

Getting Python Installed

You will need a system that has Python2.7 on it.

If you are running a recent version of MacOS X, then you will already have Python 2.7 (launch Terminal and type ‘python’ at the prompt).

Just type ‘python’ from the shell and verify it has Python2.7.

If you are running on Windows, then you should be able to download and install it from here:


Either the ‘Windows x86-64 MSI Installer’ or the ‘Windows x86 MSI Installer’ is probably the right choice.

You might also want to update your Windows system Path so that your computer knows how to find the Python interpreter.  You should be able to find this process online (for your version of Windows).


You can also use an online Python Interpreter (at least for some of the course). You can find one online at http://repl.it/languages/Python.


Network Config Templating using Ansible

The general problem is this: you want a systematic way of creating network device configurations based on templates and variables.

As background, Ansible is an open-source automation application that can be used to automate many tasks in your environment (predominantly compute and cloud tasks). Ansible can also generate files based on Jinja2 templates and variables. Jinja2 is a widely-used Python templating system, see http://jinja.pocoo.org/ for more information about Jinja2.
Thus, Ansible has all the components for network configuration templating built into it.  This is a very good initial use case for Ansible for network engineers.

Part1 – https://pynet.twb-tech.com/blog/ansible/ansible-cfg-template.html

Part2 – https://pynet.twb-tech.com/blog/ansible/ansible-cfg-template-p2.html

Part3 – https://pynet.twb-tech.com/blog/ansible/ansible-cfg-template-p3.html

Week 1

  1. Introduction and Some Questions
    video https://vimeo.com/119478712
    Length is 11 minutes
    The ‘Some Questions’ section continues into the first 1:12 of the next video
  2. Characteristics of Python
    video https://vimeo.com/119480935
    Length is 7 minutes
  3. Interpreter Shell, Variables, and Assignment
    video http://youtu.be/6ja4KlejT-g
    Length is 8 minutes
  4. Strings
    video http://youtu.be/ItIE8hItji8
    Length is 19 minutes

Week 2

  1. Introduction (Week2)
    video http://youtu.be/uqGZXPfX00E
    Length is 2 minutes
  2. Print and raw_input
    video http://youtu.be/pEXUxySxygg
    Length is 14 minutes
  3. Numbers
    video http://youtu.be/n5ZO8rRcWbA
    Length is 12 minutes
  4. Lists and Tuples
    video http://youtu.be/nUOMXXhQgZQ
    Length is 14 minutes
  5. Booleans
    video http://youtu.be/gCpRBt7pw-0
    Length is 5 minutes

Week 3

  1. Introduction Week  https://vimeo.com/120754390
  2. If Conditionals  http://youtu.be/5lHWv9hkSn8
  3. For Loops http://youtu.be/VR0ggQuClOM
  4. Passing Arguments into a Script http://youtu.be/mUt0uJmD9y4

Week 4

  1. Introduction Week 4  http://youtu.be/UJmDynoQxps
  2. While Loops http://youtu.be/xnZzrnAQdG
  3. Dictionaries http://youtu.be/iGVJmUXcLtI
  4. Exceptions http://youtu.be/fCkjmtIq7wU


In this article, I briefly introduce Python and SNMP using the pysnmp library.

I assume that you already have some knowledge on SNMP including MIBs and OIDs.  If not, you should be able to find this information fairly easily on the Internet.


In order to get started, you need to install the pysnmp library.  For context, I am testing on an AWS AMI server (RedHat based i.e. yum instead of apt).

For installation just use ‘pip’:
# As root or sudo (or in a virtual environment)
$ pip install pysnmp

I also installed net-snmp to simplify testing and to add an easy way to perform an SNMP walk.  The installation method for net-snmp will vary depending on your system.

# As root or sudo
$ yum install net-snmp
$ yum install net-snmp-utils


To keep things simple I am only going to use SNMPv1/2c (i.e. this article does not cover SNMPv3).  This is obviously not secure.


Now that I have the pysnmp library installed, the next step is to verify that I can communicate with my test router using SNMP.  First, let’s test this directly from the Linux command line:

$ snmpget -v 2c -c <COMMUNITY> <IP_ADDR> .
SNMPv2-MIB::sysDescr.0 = STRING: Cisco IOS Software, C880 Software (C880DATA-UNIVERSALK9-M), Version 15.0(1)M4, RELEASE SOFTWARE (fc1)
Technical Support: http://www.cisco.com/techsupport
Copyright (c) 1986-2010 by Cisco Systems, Inc.
Compiled Fri 29-Oct-10 00:02 by prod_rel_team
The OID . is the MIB-2 sysDecr object.  During testing I had multiple problems getting SNMP to work on the router including that I had Cisco’s Control Plane Policing enabled (ooops) and that I needed to allow access through both an edge ACL and through a separate SNMP ACL.

At this point, I am able to communicate using SNMP from my AWS server to my test router.


Now let’s try the same thing except using the pysnmp library.  In order to simplify this I have created a couple of SNMP helper functions see:


First we need to do some initialization:

>>> from snmp_helper import snmp_get_oid,snmp_extract
>>> SNMP_PORT = 161
>>> a_device = (‘’, COMMUNITY_STRING, SNMP_PORT)

This code loads my two functions (snmp_get_oid and snmp_extract); it also creates a tuple named ‘a_device’ consisting of an IP, community string, and port 161.

I then call my snmp_get_oid function using the OID of MIB-2 sysDescr:
>>> snmp_data = snmp_get_oid(a_device, oid=’.′, display_errors=True)
>>> snmp_data
[(MibVariable(ObjectName(, DisplayString(hexValue=’436973636f20494f5320536f6674776172652c204338383020536f6674776172652
I can see that I received SNMP data back albeit in an ugly format.  I can now use the snmp_extract function to display the output in a more friendly way.

>>> output = snmp_extract(snmp_data)
>>> print output
Cisco IOS Software, C880 Software (C880DATA-UNIVERSALK9-M), Version 15.0(1)M4, RELEASE SOFTWARE (fc1)
Technical Support: http://www.cisco.com/techsupport
Copyright (c) 1986-2010 by Cisco Systems, Inc.
Compiled Fri 29-Oct-10 00:02 by prod_rel_team


Now, let’s repeat this process but using a different OID.  Using snmpwalk on the ‘interfaces’ MIB and the Cisco SNMP Object Navigator, I was able to determine that the OID = . corresponded to the output octets on FastEthernet4.


Here I query that OID a couple of times in fairly quick succession (less than a minute between queries):
>>> snmp_data = snmp_get_oid(a_device, oid=’.′, display_errors=True)
>>> output = snmp_extract(snmp_data)
>>> print output

>>> snmp_data = snmp_get_oid(a_device, oid=’.′, display_errors=True)
>>> output = snmp_extract(snmp_data)
>>> print output

You can see that the count incremented.


Week 5


I. Class Review (weeks 1 – 4)
video https://vimeo.com/121951502
Length is 16 minutes




In these two exercises, I am going to use the following diagram and CDP data:
CDP data

Disclaimer: the CDP data is from a test switch and a test router. I have manually modified this data to be consistent with the above diagram.
Reference code for these exercises is posted at:


1. Parse the CDP data (see link above) to obtain the following information: hostname, ip, model, vendor, and device_type (device_type will be either ‘router’, ‘switch’, or ‘unknown’).

From this data create a dictionary of all the network devices.

The network_devices dictionary should have the following format:

network_devices = {
‘SW1’: { ‘ip’: ‘’, ‘model’: ‘WS-C2950-24’, ‘vendor’: ‘cisco’, ‘device_type’: ‘switch’ },
‘R1’: { ‘ip’: ‘’, ‘model’: ‘881’, ‘vendor’: ‘Cisco’, ‘device_type’: ‘router’ },

‘R5’: { ‘ip’: ‘’, ‘model’: ‘881’, ‘vendor’: ‘Cisco’, ‘device_type’: ‘router’ },

Note, this data structure is a dictionary that contains additional dictionaries.  The key to the outer dictionary is a hostname and the data corresponding to this key is another dictionary.  For example, for ‘R1’:

>>> network_devices[‘R1’]
{‘ip’: ‘’, ‘model’: ‘881’, ‘vendor’: ‘Cisco’, ‘device_type’: ‘router’}

You can access a given attribute in the inner dictionary as follows:
>>> a_dict[‘R1’][‘ip’]

If this is confusing, you might want to experiment with it in the Python shell:

##### Python Shell – experimenting with dictionary of dictionaries #####

# Initialize network_devices to be a blank dictionary
>>> network_devices = {}

# Assign the key ‘R1’ to network_devices using a value of a blank dictionary (in other words, I am adding a key:value pair to network_devices where the key is ‘R1’ and the value is {} )
>>> network_devices[‘R1’] = {}

# Look at network_devices at this point
>>> network_devices
{‘R1’: {}}

# Add the ‘ip’ and ‘vendor’ fields to the inner dictionary
>>> network_devices[‘R1’][‘ip’] = ‘’
>>> network_devices[‘R1’][‘vendor’] = ‘Cisco’
>>> network_devices
{‘R1’: {‘ip’: ‘’, ‘vendor’: ‘Cisco’}}

##### Python Shell – experimenting end #####

For the output to this exercise, print network_devices to standard output.  Your output should look similar to the following (six network devices with their associated attributes).

{‘R1’: {‘device_type’: ‘Router’,
‘ip’: ‘’,
‘model’: ‘881’,
‘vendor’: ‘Cisco’},
‘R2’: {‘device_type’: ‘Router’,
‘ip’: ‘’,
‘model’: ‘881’,
‘vendor’: ‘Cisco’},
‘R3’: {‘device_type’: ‘Router’,
‘ip’: ‘’,
‘model’: ‘881’,
‘vendor’: ‘Cisco’},
‘R4’: {‘device_type’: ‘Router’,
‘ip’: ‘’,
‘model’: ‘881’,
‘vendor’: ‘Cisco’},
‘R5’: {‘device_type’: ‘Router’,
‘ip’: ‘’,
‘model’: ‘881’,
‘vendor’: ‘Cisco’},
‘SW1’: {‘device_type’: ‘Switch’,
‘ip’: ‘’,
‘model’: ‘WS-C2950-24’,
‘vendor’: ‘cisco’}}

2. Create a second program that expands upon the program from exercise 1.

This program will keep track of which network devices are physically adjacent to each other (physically connected to each other).

You will accomplish this by adding a new field (adjacent_devices) to your network_devices inner dictionary.  adjacent_devices will be a list of hostnames that a given network device is physically connected to.  

For example, the dictionary entries for ‘R1’ and ‘SW1’ should look as follows:

‘R1’: {‘IP’: ‘’,
‘adjacent_devices’: [‘SW1’],
‘device_type’: ‘Router’,
‘model’: ‘881’,
‘vendor’: ‘Cisco’},

‘SW1’: {‘IP’: ‘’,
‘adjacent_devices’: [‘R1’, ‘R2’, ‘R3’, ‘R4’, ‘R5’],
‘device_type’: ‘Switch’,
‘model’: ‘WS-C2950-24’,
‘vendor’: ‘cisco’},


For the output to this exercise, print network_devices to standard output.  Your output should look similar to the following (six network devices with their associated attributes including ‘adjacent_devices’).

{‘R1’: {‘IP’: ‘’,
‘adjacent_devices’: [‘SW1’],
‘device_type’: ‘Router’,
‘model’: ‘881’,
‘vendor’: ‘Cisco’},
‘R2’: {‘IP’: ‘’,
‘adjacent_devices’: [‘SW1’],
‘device_type’: ‘Router’,
‘model’: ‘881’,
‘vendor’: ‘Cisco’},
‘R3’: {‘IP’: ‘’,
‘adjacent_devices’: [‘SW1’],
‘device_type’: ‘Router’,
‘model’: ‘881’,
‘vendor’: ‘Cisco’},
‘R4’: {‘IP’: ‘’,
‘adjacent_devices’: [‘SW1’],
‘device_type’: ‘Router’,
‘model’: ‘881’,
‘vendor’: ‘Cisco’},
‘R5’: {‘IP’: ‘’,
‘adjacent_devices’: [‘SW1’],
‘device_type’: ‘Router’,
‘model’: ‘881’,
‘vendor’: ‘Cisco’},
‘SW1’: {‘IP’: ‘’,
‘adjacent_devices’: [‘R1’, ‘R2’, ‘R3’, ‘R4’, ‘R5’],
‘device_type’: ‘Switch’,
‘model’: ‘WS-C2950-24’,
‘vendor’: ‘cisco’}}




Video Archive

Introduction and Some Questions
What is the Nature of Python
Interpreter Shell, Variables, and Assignment

Print and raw_input
Lists and Tuples

If Conditionals
For Loops
Passing Arguments into a Script

While Loops




Kirk Byers
Twitter: @kirkbyers


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s