Filter out Public IPs from a list – simple Python script

In this lab, we are going to use a python script to filter out public IPs from a list of IP addresses.

Let’s review a simple IP addressing concept. To conserve IPv4 addresses certain blocks of IPs have been curved out of the IPv4 address ranges for use as private IPs.

Public IPs are routeable over the internet while private IPs are only significant within the scope of you Private/Local Area Network

IPv4 addresses are separated into 5 classes

With this in mind, we can write a very simple piece of code to enable us filter out Public IPs from a list of IPs. Unlike a lot of other fields where programming is used, as you continue on your journey of Network Automation programming with Python, you will realize that a lot of the scripts you will be writing will be to manipulate strings. So, it will be vital to understand as many of the functions and methods that are used on strings.

startswith() and endswith() methods

The startswith() method looks at a string and confirms if it begins a specified series of characters.


>>>str1 = “Hello”

The endswith() method looks at a string and confirms if it ends a specified series of characters


>>>str1 = “Hello”
>>> str1.endswith(“o”)

Let’s take advantage of this methods and look through a list containing some IP addresses.

mylist = ["", "", "", "", 
          "", ""]

Here is the code we will be using.

Note: line 18,19 and 20 should be one continuous line


Step 1. Create some empty list which we shall use to hold our outputs after running our code.

startswith_10 = []
endswith_1 = []
publicips = []


Step 2. Use a for loop to iterate through the elements of the mylist[] list. For each iteration 3 other if, else loops will run . Let’s start with the first if,else loop and see what it does.

for x in mylist:
	ipadd = x
	#print(ipadd)    #run this to confirm all elements of mylist       
	if ipadd.startswith("10"):

The first element of the mylist[] list is a string “”, This element from the for loop is a variable x. To make the code easier to understand we assign it to another variable ipadd. Then our code applies the startswith(“10”) method on our string “”. We have defined a parameter of “10” within the method. This statement is True and as such we can continue with the action specified within the loop.

startswith_10.append(ipadd). startswith_10 is a list. List have an inbuilt function append() that allows you to add an element to the tail end of a series of elements within a list. As our list is currently empty, the string is added onto it.

Step 3. Still within the for loop and still on the first element of our list “” we then move to the next if, else loop.

	if ipadd.endswith("1"):

Does our current element qualify as above? Does the endswith(“1”) return a True. Yes, it does. So again using the append() list function we add this string to the endswith_1 list.

Step 4. Now the final if, else loop.

if ipadd.startswith("10") or ipadd.startswith("192.168") or 
      ipadd.startswith("172.16") or ipadd.startswith("172.17") 
      or ipadd.startswith("172.18") or ipadd.startswith("172.19")
      or ipadd.startswith("172.2") or ipadd.startswith("172.30") 
      or ipadd.startswith("172.31"):

For this step we are running a number of comparisons on our element “” . Let’s take a moment to discuss the or Keyword. or is an operator that belongs to a group known as Logical Operators.

The first operand is the statement ipadd.startswith(“10”). Does the element pass this test and return a True? Yes is does, so there is actually no need to continue with more comparisons with the rest of the operands. The next section on the if statement is another Keyword pass. This key word causes no code to be executed. Essentially here because our element has passed the test nothing happens.

We will look at the other 2 keyword in upcoming blogs

The next element to go through the loops will be “”. It fails the first test, so it doesn’t get placed in the startswith_10[] list. However it passes the second test and gets placed in the endswith_1[] list. Does it pass the third test? Does it get placed in the publicips[] list?

How about “” and “”?

Both of them will pass the third test, and will therefore be placed in the publicips[] list . Notice that they fail the if section of the third if else statement, and as such the pass does not come into effect, the code then moves to the else section of that loop and here publicips.append(ipadd) runs and the 2 IPs are added to the list.

Step 5. Output from the code.

print("All IPs starting with 10 are: " + str(startswith_10))  
print("All IPs ending with 1 are: "+ str(endswith_1))
print("All the public IPs are: " + str(publicips))

Do you know why we have the str( ) function?


Networks Engineering, Networks Infrastructure Security, Networks Automation

Leave a Reply

Your email address will not be published.