Python for Network Engineers

How to Generate, Filter, Exclude IP Addresses Using List Comprehensions and More

By George El. • February 2019 • Reading time: 5 minutes

In this post, I will show how to create a list of ip addresses using list comprehensions and also using the ipaddress module in python 3. Put it simply List Comprehensions is a way to create a list from another list or iterable. They provide an alternative to filter and map functions that can be found in other languages and python

How to Read and Write Json Files

By George El. • February 2019 • Reading time: 2 minutes

In this post, I will show how to read, write and do operations on json data. I assume the I have the following json file. The first step is to make sure that this is valid json. There are many online validators, this is the one I recommend: https://jsoneditoronline.org/ This can also prettify you json to make it more readable.

How to Ping Multiple Addresses Asynchronously Part I

By George El. • February 2019 • Reading time: 2 minutes

In this post, I will show you how to ping many ip addresses synchronously, or asynchronously using threads. the following examples work only with python 3. First I will use a list comprehension to create a list of ip addresses ips = ["192.168.2.{}".format(i) for i in range(1,11)] the above code will create addresses 192.

How to Sort IP Addresses With Python

By George El. • January 2019 • Reading time: 2 minutes

Python provides a a sort and sorted function. Sort changes the original list, while sorted creates a copy of the list, without changing the original list. So the sort version is faster, but it works only on lists, while sorted works on any iterable. Now the problem is that if I try to sort IPs like strings, they won’t be sorted properly.

Using Python to Remove Passwords and IP Addresses From Configuration Files

By George El. • January 2019 • Reading time: 4 minutes

Lets assume you have a configuration file and you want to send it to someone, but you want to remove the passwords, and ip addresses before you send it. the program takes one argument, the config to be read import sys import re if len(sys.argv) < 2: print("please type a configuration file: replaceIPs.

How to Sort Vlans

By George El. • January 2019 • Reading time: 1 minute

Recently I had a list of iterface vlans, that I wanted to sort. First I tried the sorted method but this didn’t work. Lets have a look: >>> l=['vlan101','vlan201','vlan1','vlan2','vlan3','vlan4','vlan10','vlan11','vlan102','vlan202','vlan301','vlan302'] >>> for vlan in sorted(l): ... print(vlan) ... vlan1 vlan10 vlan101 vlan102 vlan11 vlan2 vlan201 vlan202 vlan3 vlan301 vlan302 vlan4 You see?