This post will describe the exercises and solutions for week three of Kirk Byers Python for Network Engineers.
Exercise two of week three we already completed in a previous post where we used a For loop to loop through the BGP output.
Let’s move on to exercise three instead:
III. You have the following 'show ip int brief' output. show_ip_int_brief = ''' Interface IP-Address OK? Method Status Protocol FastEthernet0 unassigned YES unset up up FastEthernet1 unassigned YES unset up up FastEthernet2 unassigned YES unset down down FastEthernet3 unassigned YES unset up up FastEthernet4 6.9.4.10 YES NVRAM up up NVI0 6.9.4.10 YES unset up up Tunnel1 16.25.253.2 YES NVRAM up down Tunnel2 16.25.253.6 YES NVRAM up down Vlan1 unassigned YES NVRAM down down Vlan10 10.220.88.1 YES NVRAM up up Vlan20 192.168.0.1 YES NVRAM down down Vlan100 10.220.84.1 YES NVRAM up up ''' From this output, create a list where each element in the list is a tuple consisting of (interface_name, ip_address, status, protocol). Only include interfaces that are in the up/up state. Print this list to standard output.
In this exercise, Kirk is using pprint to print text so the first thing we need to do is to import this module:
import pprint
Currently we have a massive string of information. Let’s print this string to see what it looks like:
daniel@daniel-iperf3:~/python/Week3$ python3 show_ip.py ('\n' 'Interface IP-Address OK? Method Status ' 'Protocol\n' 'FastEthernet0 unassigned YES unset up up\n' 'FastEthernet1 unassigned YES unset up up\n' 'FastEthernet2 unassigned YES unset down down\n' 'FastEthernet3 unassigned YES unset up up\n' 'FastEthernet4 6.9.4.10 YES NVRAM up up\n' 'NVI0 6.9.4.10 YES unset up up\n' 'Tunnel1 16.25.253.2 YES NVRAM up down\n' 'Tunnel2 16.25.253.6 YES NVRAM up down\n' 'Vlan1 unassigned YES NVRAM down down\n' 'Vlan10 10.220.88.1 YES NVRAM up up\n' 'Vlan20 192.168.0.1 YES NVRAM down down\n' 'Vlan100 10.220.84.1 YES NVRAM up up\n')
That’s one giant string! We want to break up this giant string into a list of strings based on “\n”. As you saw above there each line has a “\n” before the next one starts. Let’s print the output of the list and the class type to show it’s a list.
show_ip_lines = show_ip_int_brief.split("\n") print(show_ip_lines) print(type(show_ip_lines))
If we run the script this is what we have so far:
daniel@daniel-iperf3:~/python/Week3$ python3 show_ip.py ['', 'Interface IP-Address OK? Method Status Protocol', 'FastEthernet0 unassigned YES unset up up', 'FastEthernet1 unassigned YES unset up up', 'FastEthernet2 unassigned YES unset down down', 'FastEthernet3 unassigned YES unset up up', 'FastEthernet4 6.9.4.10 YES NVRAM up up', 'NVI0 6.9.4.10 YES unset up up', 'Tunnel1 16.25.253.2 YES NVRAM up down', 'Tunnel2 16.25.253.6 YES NVRAM up down', 'Vlan1 unassigned YES NVRAM down down', 'Vlan10 10.220.88.1 YES NVRAM up up', 'Vlan20 192.168.0.1 YES NVRAM down down', 'Vlan100 10.220.84.1 YES NVRAM up up', '']
So currently we have a list of information about the interface name, IP etc.
The next step is to create an empty list that we will append information to after we extract it from this current list:
show_ip_list = []
What we want to do now is to loop through our list and get some information out of it. We use a For loop to loop through the list:
for line in show_ip_lines:
We don’t want any data from the line that has “Interface”, “IP-Address” in it, the header line. We use an if statement to match this and “continue” the loop if we find this string. When using “continue” we can hand the control back to the loop not doing anything in that iteration.
if "Interface" in line: continue
To get information from each line we will split the lines based on white space.
line_split = line.split()
To not include lines that don’t have the correct number of fields we will do a check to see if the number of fields is 6.
if len(line_split) == 6:
Assuming that our list is the correct length, we want to map the information in the list into variables. Note that two variables are called “discard1” and “discard2” because we are not interested in keeping this information:
ifname, ip_add, discard1, discard2, line_status, line_proto = line_split
We only want to keep the lines where the line status is up and the line protocol is up. We use an if statement with “and” to make sure both are set to “up”.
if (line_status == "up") and (line_proto == "up"):
After this check we want to append this information to the list we created before “show_ip_list”.
show_ip_list.append((ifname, ip_add, line_status, line_proto))
Then all that is left is to do some printing, using the pretty print module we imported before.
print("\n") pprint.pprint(show_ip_list) print("\n")
If we run this script this is the result:
daniel@daniel-iperf3:~/python/Week3$ python3 show_ip.py [('FastEthernet0', 'unassigned', 'up', 'up'), ('FastEthernet1', 'unassigned', 'up', 'up'), ('FastEthernet3', 'unassigned', 'up', 'up'), ('FastEthernet4', '6.9.4.10', 'up', 'up'), ('NVI0', '6.9.4.10', 'up', 'up'), ('Vlan10', '10.220.88.1', 'up', 'up'), ('Vlan100', '10.220.84.1', 'up', 'up')]
Code available at Github. See you next time!