This post will describe the exercises and solutions for week three of Kirk Byers Python for Network Engineers.

The last exercise of the week is to create an IP address checker that checks the validity of an IP address. Here are the instructions:

IV. Create a script that checks the validity of an IP address.  The IP address should be supplied on the command line.
    A. Check that the IP address contains 4 octets.
    B. The first octet must be between 1 - 223.
    C. The first octet cannot be 127.
    D. The IP address cannot be in the 169.254.X.X address space.
    E. The last three octets must range between 0 - 255.

    For output, print the IP and whether it is valid or not.

The IP address will be supplied through the command line. Like we’ve done before we are going to check the number of arguments supplied and exit the script if the number of arguments is not two. We need to import sys so that we can use sys.argv.

import sys

if len(sys.argv) != 2:
	sys.exit("Usage: ./ip_checker.py <IP-ADDRESS>")

We’ll store the IP address as input as dotted decimal into the variable ip_add. We get this value by popping the last string in the sys.argv list.

ip_add = sys.argv.pop()

We will use a variable called valid_ip to indicate if the IP address is good or not. We se this to True and change the value to False if certain conditions are met.

valid_ip = True

We will then split the IP address into octets and make sure that the number of octets is four. If it’s not the script will exit.

if len(octets) != 4:
	sys.exit("The number of octets is invalid.")

The next step is to convert our strings in octets into integers. We will use a For loop to do this but we will use enumerate() to give us an index. We need the index so that we can run the int() function on the different slices in the list. We use try/except to exit out of the script if the conversion to integers is not successful.

for index, octet in enumerate(octets):
	try:
		octets[index] = int(octet)
	except ValueError:
		sys.exit("\n\nInvalid IP address: {}\n".format(ip_add))

We will then store each slice of the octets list in its own variable.

first_octet, second_octet, third_octet, fourth_octet = octets

We then check the validity of the first octet.

if first_octet < 1:
	valid_ip = False
elif first_octet > 223:
	valid_ip = False
elif first_octet == 127:
	valid_ip = False

When that’s done we check that the address is not from the 169.254.0.0/16 range.

if first_octet == 169 and second_octet == 254:
	valid_ip = False

All that is left now is to check that the rest of the octets are not less than 0 or larger than 255. We use a For loop to iterate through our integers.

for octet in (second_octet, third_octet, fourth_octet):
	if (octet < 0) or (octet > 255):
		valid_ip = False

The final part is to print if it’s a valid IP or not.

if valid_ip:
	print("\n\nThe IP address is valid: {}".format(ip_add))
else:
	sys.exit("\n\nInvalid IP address: {}".format(ip_add))

Then we try to run the script with some valid and invalid IP addresses:

[email protected]:~/python/Week3$ python3 ip_checker.py 169.254.0.1


Invalid IP address: 169.254.0.1
[email protected]:~/python/Week3$ python3 ip_checker.py 127.0.0.1


Invalid IP address: 127.0.0.1
[email protected]:~/python/Week3$ python3 ip_checker.py 224.0.0.1


Invalid IP address: 224.0.0.1
[email protected]:~/python/Week3$ python3 ip_checker.py 192.168.0.1


The IP address is valid: 192.168.0.1
[email protected]:~/python/Week3$ python3 ip_checker.py 192.168.300.1


Invalid IP address: 192.168.300.1

This looks good! Code at Github. See you next time!

Python – Kirk Byers Course Week 3 Part 3

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: