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:

[code lang=text]
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.
[/code]

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.

[python]
import sys

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

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.

[python]
ip_add = sys.argv.pop()
[/python]

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.

[python]
valid_ip = True
[/python]

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.

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

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.

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

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

[python]
first_octet, second_octet, third_octet, fourth_octet = octets
[/python]

We then check the validity of the first octet.

[python]
if first_octet < 1:
valid_ip = False
elif first_octet > 223:
valid_ip = False
elif first_octet == 127:
valid_ip = False
[/python]

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

[python]
if first_octet == 169 and second_octet == 254:
valid_ip = False
[/python]

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.

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

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

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

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

[code lang=text]
daniel@daniel-iperf3:~/python/Week3$ python3 ip_checker.py 169.254.0.1

Invalid IP address: 169.254.0.1
daniel@daniel-iperf3:~/python/Week3$ python3 ip_checker.py 127.0.0.1

Invalid IP address: 127.0.0.1
daniel@daniel-iperf3:~/python/Week3$ python3 ip_checker.py 224.0.0.1

Invalid IP address: 224.0.0.1
daniel@daniel-iperf3:~/python/Week3$ python3 ip_checker.py 192.168.0.1

The IP address is valid: 192.168.0.1
daniel@daniel-iperf3:~/python/Week3$ python3 ip_checker.py 192.168.300.1

Invalid IP address: 192.168.300.1
[/code]

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 *