Python3 // Lesson 2: Conditionals and Loops

  1. Conditionals
    Video https://vimeo.com/245104620
    Length is 8 minutes
     
  2. Boolean Logic (Booleans, Ternary Operator, None)
    Video https://vimeo.com/245112558
    Length is 8 minutes
     
  3. Python For Loops
    Video https://vimeo.com/245466297
    Length is 5 minutes
     
  4. For Loops (Enumerate)
    Video https://vimeo.com/245477015
    Length is 6 minutes
     
  5. For Loops (Break and Continue)
    Video https://vimeo.com/245478016
    Length is 9 minutes
     
  6. While Loops
    Video https://vimeo.com/245545155
    Length is 5 minutes
     
  7. Loops Miscellaneous
    Video https://vimeo.com/245552604
    Length is 6 minutes

Additional Content:

How To Write Conditional Statements in Python 3​​

How To Construct For Loops in Python 3

How To Construct While Loops in Python 3

Exercises

Reference code for these exercises is posted on GitHub at:
https://github.com/ktbyers/pynet/tree/master/learning_python/lesson3

1. Read the “show_vlan.txt” file into your program. Loop through the lines in this file and extract all of the VLAN_ID, VLAN_NAME combinations. From these VLAN_ID and VLAN_NAME construct a new list where each element in the list is a tuple consisting of (VLAN_ID, VLAN_NAME). Print this data structure to the screen. Your output should look as follows:

[('1', 'default'),
 ('400', 'blue400'),
 ('401', 'blue401'),
 ('402', 'blue402'),
 ('403', 'blue403')]

2. Read the contents of the “show_arp.txt” file. Using a for loop, iterate over the lines of this file. Process the lines of the file and separate out the ip_addr and mac_addr for each entry into a separate variable.

Add a conditional statement that searches for ‘10.220.88.1’. If 10.220.88.1 is found, print out the string “Default gateway IP/Mac” and the corresponding IP address and MAC Address.

Using a conditional statement, also search for ‘10.220.88.30’. If this IP address is found, then print out “Arista3 IP/Mac is” and the corresponding ip_addr and mac_addr.

Keep track of whether you have found both the Default Gateway and the Arista3 switch. Once you have found both of these devices, ‘break’ out of the for loop.

3.  Read the ‘show_lldp_neighbors_detail.txt’ file. Loop over the lines of this file. Keep reading the lines until you have encountered the remote “System Name” and remote “Port id”. Save these two items into variables and print them to the screen. You should extract only the system name and port id from the lines (i.e. your variables should only have ‘twb-sf-hpsw1′ and ’15’). Break out of your loop once you have retrieved these two items.

4. You have the following data structure:

arp_table = [('10.220.88.1', '0062.ec29.70fe'),
 ('10.220.88.20', 'c89c.1dea.0eb6'),
 ('10.220.88.21', '1c6a.7aaf.576c'),
 ('10.220.88.28', '5254.aba8.9aea'),
 ('10.220.88.29', '5254.abbe.5b7b'),
 ('10.220.88.30', '5254.ab71.e119'),
 ('10.220.88.32', '5254.abc7.26aa'),
 ('10.220.88.33', '5254.ab3a.8d26'),
 ('10.220.88.35', '5254.abfb.af12'),
 ('10.220.88.37', '0001.00ff.0001'),
 ('10.220.88.38', '0002.00ff.0001'),
 ('10.220.88.39', '6464.9be8.08c8'),
 ('10.220.88.40', '001c.c4bf.826a'),
 ('10.220.88.41', '001b.7873.5634')] 

Loop over this data structure and extract all of the MAC addresses. Process all of the MAC addresses to get them into a standard format. Print all of the new standardized MAC address to the screen. The standardized format should be as follows:

00:62:EC:29:70:FE

The hex digits should be capitalized. Additionally, there should be a colon between each octet in the MAC address.

5. [Optional/bonus] 

*** Note, to actually test this in your environment, change the test IP addresses to something in your environment that you can ping successfully. ***

Construct a list of 254 IP addresses. The base IP address should be equal to ‘10.10.100.0’ or ‘10.10.100.’.

You should use the ‘range’ builtin to accomplish this.

Your list should have all of the IP addresses from 10.10.100.1 to 10.10.100.254.

Use Python’s ‘enumerate’ to print out all of the IP addresses and their corresponding list index. The output should look similar to the following: 

0 ---> 10.10.100.1
1 ---> 10.10.100.2
2 ---> 10.10.100.3
3 ---> 10.10.100.4
4 ---> 10.10.100.5
...

Use a list slice to create a new list that goes from 10.10.100.3 to 10.10.100.6.

Using a loop and os.system(“ping -c 3 10.10.100.3”) try pinging all of the IP addresses in this short list. For Windows the command will probably be os.system(“ping -n 3 10.10.100.3”).

Put a variable at the top to define whether you are using Windows or Linux/MacOs. This should be similar to the following:

WINDOWS = False

base_cmd_linux = 'ping -c 2'
base_cmd_windows = 'ping -n 2'
# Ternary operator
base_cmd = base_cmd_windows if WINDOWS else base_cmd_linux