This post will describe the exercises and solutions for week two of Kirk Byers Python for Network Engineers.
This is the first exercise:
I. Create a script that does the following A. Prompts the user to input an IP network. Notes: 1. For simplicity the network is always assumed to be a /24 network 2. The network can be entered in using one of the following three formats 10.88.17.0, 10.88.17., or 10.88.17 B. Regardless of which of the three formats is used, store this IP network as a list in the following format ['10', '88', '17', '0'] i.e. a list with four octets (all strings), the last octet is always zero (a string). Hint: There is a way you can accomplish this using a list slice. Hint2: If you can't solve this question with a list slice, then try using the below if statement (note, we haven't discussed if/else conditionals yet; we will talk about them in the next class). >>>> CODE <<<< if len(octets) == 3: octets.append('0') elif len(octets) == 4: octets[3] = '0' >>>> END <<<< C. Print the IP network out to the screen. D. Print a table that looks like the following (columns 20 characters in width): NETWORK_NUMBER FIRST_OCTET_BINARY FIRST_OCTET_HEX 88.19.107.0 0b1011000 0x58
To get input from an user we need to use the "input()" function. In legacy Python the function is called "raw_input()".
ip_add = input("Enter an IP address: ")
The result of what the user inputs is stored in the variable "ip_add". The text within the paranthesis is what we want to display to the user so that we get input back from the user. This is what the script looks like so far:
daniel@daniel-iperf3:~/python/Week2$ python3 ip_input.py Enter an IP address: 192.168.10.0
Let's first print out the variable and check that it's a string.
ip_add = input("Enter an IP address: ") print(ip_add) print(type(ip_add))
This produces the following result:
daniel@daniel-iperf3:~/python/Week2$ python3 ip_input.py Enter an IP address: 192.168.10.0 192.168.10.0
The next step is to store the IP network as a list consisting of strings. We will use the built-in function "split()" to split the IP address into octets and tell Python that a dot is the delimiter.
ip_octet = ip_add.split(".")
We can also print the contents and check that we have a list.
ip_octet = ip_add.split(".") print(ip_octet) print(type(ip_octet))
This then gives the following input:
daniel@daniel-iperf3:~/python/Week2$ python3 ip_input.py Enter an IP address: 192.168.10.0 ['192', '168', '10', '0']
We were told though to only type in the first three octets so currently we have no control of what the user types in the fourth octet. We want to discard that part. We can use list slicing to accomplish this. When using slicing it is zero indexed so the first string in the list is 0, the second one is 1, the third one is 2. It's also important to note that the end index is non inclusive so if the end index is 3, only index 0, 1 and 2 will be included. The syntax to use list slicing is "listname[startindex:endindex:stepping]". Our code then becomes the following:
ip_octet = ip_octet[:3]
The syntax here is that we want to start at index 0, this is indicated by ":", the last index is 3, non inclusive so the last one to enter the list is actually 2. It would also have been possible to do "ip_octet = ip_octet[:-1]". This would have ignored the last string in the list("-1") but our current code is better since it would protect against the user entering more octets than four.
The next step is to add a string to our list. Adding something can also be called appending. There is a built-in function called "append()" which will help us here. We will add the string "0" to the list.
ip_octet.append("0")
This is what the variable ip_octet consists of right now:
daniel@daniel-iperf3:~/python/Week2$ python3 ip_input.py Enter an IP address: 192.168.10.0 ['192', '168', '10', '0']
This looks good so far. The next task is to print the network out to the console. We need to use "join()" to join the octets back into an IP address. We will insert a dot between the strings when joining them.
ip_new = ".".join(ip_octet) print("Your network is: {}".format(ip_new))
I'm using Python 3.6 syntax here. For older versions the "{}" might have to be indicated as "{0}" and legacy Python uses "%s" for strings instead of the newer ".format" syntax.
This looks good so far:
daniel@daniel-iperf3:~/python/Week2$ python3 ip_input.py Enter an IP address: 192.168.10.0 Your network is: 192.168.10.0
The next step is to print a table with the network number, first octet in binary and first octet in hex. There are built-in functions to convert integers into binary or hex. However, currently we only have a string. Let's try to print the binary number of our network number.
print(bin(ip_new))
This however doesn't work so well...
daniel@daniel-iperf3:~/python/Week2$ python3 ip_input.py Enter an IP address: 192.168.10.0 Your network is: 192.168.10.0 Traceback (most recent call last): File "ip_input.py", line 17, inprint(bin(ip_new)) TypeError: 'str' object cannot be interpreted as an integer
The string must first be converted into an integer. The code to do this is "bin(int(string_name))". We also want to print a pretty table. We will use ".format" to format and make it left aligned. This is indicated by "{:<20}" in the code where the "<" indicates left aligned and 20 is the width of our column. This is then the code we need to put in.
first_octet_bin = bin(int(ip_octet[0])) first_octet_hex = hex(int(ip_octet[0])) print("\n\n") print("{:<20} {:<20} {:<20}".format("NETWORK_NUMBER", "FIRST_OCTET_BINARY", "FIRST_OCTET_HEX")) print("{:<20} {:<20} {:<20}".format(ip_new, first_octet_bin, first_octet_hex))
We store the first octet in binary and hex in the new variables. We use the built-in functions "bin()" and "hex()" but we run them through "int()" since we had a string to beging with. As you can see it's possible to run a function in Python on input from another function. We are using list slicing to pick out the first string from the list "ip_octet".
We then print some "newlines" and then print the table referring to the variables above. The final result is then this:
daniel@daniel-iperf3:~/python/Week2$ python3 ip_input.py Enter an IP address: 192.168.10.0 Your network is: 192.168.10.0 NETWORK_NUMBER FIRST_OCTET_BINARY FIRST_OCTET_HEX 192.168.10.0 0b11000000 0xc0
The code is as usual available at my Github. See you next time!
This iis very fascinating, You’re an excessively professional
blogger. I have joined your rsss feed and sta up for looking for more of
your great post. Also, I’ve shared your website in my
social networks
Output of “print(type(ip_add))” is not shown above.