Home > Python, Routing > Learning Python: Week3 (Conditionals and For Loops) -Part 3

Learning Python: Week3 (Conditionals and For Loops) -Part 3

As discussed in post  ( https://crazyrouters.wordpress.com/2017/02/25/learning-python-kirk-byers-python-course/  ) , i will be sharing the my learning on weekly basis as course continues. This will not only motivate me but also help others who are in phase of learning python 3.

This post will focus on Week 3 (Conditionals and For Loops) .This post will focus on exercise 2

################EXERCISE 2 #########################

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.

##################### END #######################

Here we have been provided with huge string having the “sh ip int brief” command output.We have to create a list of up interfaces only.

First we will seperate each line by using “\n” in  string split method

out = show_ip_int_brief.split(‘\n’)

>>> 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
'''
>>> out = show_ip_int_brief.split('\n')
>>>
>>> print(out)
['', '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 we get list out [] having  elements  as above. We can see first and last element as blank space.lets remove the first and last element

>>> out1 =out[1:-1]
>>>
>>> print(out1)
['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 now we have get the new list out1 , now we will work upon the list out1.

We will take each element and split further using split() method which will split each element futher so example ‘FastEthernet0 unassigned YES unset up up’ is element which will be further split into ‘FastEthernet0’ ‘unassigned’ ‘YES’ ‘unset’ ‘up’ ‘up’.

we will use for loop to execute the same. But before that we can see that we element[0] i.e ‘Interface IP-Address OK? Method Status Protocol’ is of no use , we don’t need any data from it. So we can continue to next element and then perform split operation next element onwards.

>>> for i , element in enumerate(out1):
if "interface" in element:
countinue

out_split = element.split()

We want the only Interace, Ip-Address ,Staus and Protocol , lets store the required out in variable

if_name, ip_addr, OK?, Method, line_status, line_proto = out_split

Last part is to discard Ok? and Method  , that can be done appending the only required value in respective variable, that can be done by appending into new list line[].

if_name, ip_addr, OK, Method, line_status, line_proto = out_split
if (line_status == 'up') and (line_proto == 'up'):
line.append((if_name, ip_addr, line_status, line_proto))

Now we have to print the output in more better way using pprint() function.

pprint.pprint(line)

So Here is final code and  corresponding output.

Code:

import pprint

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
'''

out = show_ip_int_brief.split('\n')

#print(out)

line =[]

out1 =out[1:-1]
for i , element in enumerate(out1):
if "interface" in element:
countinue

out_split = element.split()

if_name, ip_addr, OK, Method, line_status, line_proto = out_split
if (line_status == 'up') and (line_proto == 'up'):
line.append((if_name, ip_addr, line_status, line_proto))

pprint.pprint(line)

 

OUTPUT:

[('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')]

 

So here we are done with the Exercise 2 , will be back with exercise 3.

Smiles 🙂

 

 

 

 

 

 

Advertisements
Categories: Python, Routing Tags: ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: