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

The first exercise of the week is the following:

The first thing we want to do is to import the module sys because we are going to be working with command line arguments.

When the script is run the first argument of “sys.argv” (index 0) is the script name itself. The second argument (index 1) is the argument we supply to the script. Let’s prove this with the following code:

When this code is run we can see that “sys.argv” is a list:

When this script is run, there should be exactly two arguments given. The script name itself and then the IP address. If there were less than two arguments then only the script name was supplied and if more than two arguments were supplied too many arguments were supplied.

How can we check how many arguments were supplied? We can check the length of “sys.argv” since it’s a list. The length should be two. If it’s not we will use “sys.exit” to display a message about the usage of the script and exit.

If we run the script with too few or too many arguments the script will exit and print a help text.

The next thing we want to do is store the IP address that was entered when the script was run. We can use Pythons bult-in “pop()” function for this. “pop()” when no index is given will remove the last item from a list and return it. It would also be possible to refer to “sys.argv[1]” if not using “pop()”.

Then we take another variable called “octets” and run “split()” on “ip_address” to store the octets of the IP address there. The delimiter is a dot.

The next thing we want to do is to check that it’s a validly formatted IP address, meaning that we should have four octets. This is not a full check but prevents someone from entering an IP like “192.168.1.1.1”. If the length of “octets” is not 4 the script will exit. I’m printing the length of “octets” if the script exits just to demonstrate but it won’t be in the final code.

This produces the following output if we enter a too long IP address.

What we need to do next is to loop through each octet(string) that is stored in the list “octets”. Let’s first print the content of “octets” and the type so that you can follow along.

Running this then produces the following output:

We create an empty list to which we will append our octets later:

To loop through the contents of “octets” we use a For loop:

Then we want to convert the octet to binary. We can’t run “bin()” on a string though so first we must turn the string into an integer.

We were told to strip off “0b” from our binary number which will be there by default. This is a prefix and it’s two characters long. So say that we have the binary number 16. It would be represented like this: “0b10000”. If we remove the first two chars, meaning index 0 and 1 from the string we will have a string without “0b”. Do note that “bin_octet” is a string which is why we can use slicing.

In case our binary string isn’t 8 chars long we need to pad it with zeroes. Since the length of “bin_octet” can vary a For loop is not really suitable. We will use a While loop instead. A “while True” loop can be used to loop until some condition becomes True and the loop “breaks”.

This While loop will run indefinitely if we don’t break it since we are using the “while True” syntax.

The string “bin_octet” will be padded with zeroes until it is 8 chars long:

We concatenate to prepend a “0” to “bin_octet” until it becomes the right length.

We then append this octet after padding to the empty list we created earlier called “ip_addr_bin”:

The next step is to put all of the octets together with a dot as the delimiter.

Finally we print a pretty table with the IP address and its binary representation:

The end result is this:

The code is available at Github and thanks for reading! See you next time!

Python – Kirk Byers Course Week 3 Part 1

Leave a Reply

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

%d bloggers like this: