Length is 6 minutes
- Dictionaries Methods
Length is 7 minutes
Length is 9 minutes
Length is 15 minutes
- Regular Expressions (Part1)
Length is 15 minutes
- Regular Expressions (Part2)
Length is 7 minutes
- Regular Expressions (Part3)
Length is 8 minutes
- Regular Expressions, Other Methods
Length is 4 minutes
Regular Expression Tutorial
This is a good resource if you are new to regular expressions.
Online Regular Expression Tester
Select ‘Python’ on the left-hand side.
Python Regular Expression HowTo
This is a good overview of regular expression special characters.
Start at the very top of the page and read through the ‘Repeating Things’ section.
Automate the Boring Stuff – Dictionaries and Structuring Data
Read through the ‘The GET() Method’ section.
Reference code for these exercises is posted on GitHub at:
1. Create a dictionary representing a network device. The dictionary should have key-value pairs representing the ‘ip_addr’, ‘vendor’, ‘username’, and ‘password’ fields.
Print out the ‘ip_addr’ key from the dictionary.
If the ‘vendor’ key is ‘cisco’, then set the ‘platform’ to ‘ios’. If the ‘vendor’ key is ‘juniper’, then set the ‘platform’ to ‘junos’.
Create a second dictionary named ‘bgp_fields’. The ‘bgp_fields’ dictionary should have a keys for ‘bgp_as’, ‘peer_as’, and ‘peer_ip’.
Using the .update() method add all of the ‘bgp_fields’ dictionary key-value pairs to the network device dictionary.
Using a for-loop, iterate over the dictionary and print out all of the dictionary keys.
Using a single for-loop, iterate over the dictionary and print out all of the dictionary keys and values.
2. Create three separate lists of IP addresses. The first list should be the IP addresses of the Houston data center routers, and it should have over ten RFC1918 IP addresses in it (including some duplicate IP addresses).
The second list should be the IP addresses of the Atlanta data center routers, and it should have at least eight RFC1918 IP addresses (including some addresses that overlap with the Houston data center).
The third list should be the IP addresses of the Chicago data center routers, and it should have at least eight RFC1918 IP addresses. The Chicago IP addresses should have some overlap with both the IP addresses in Houston and Atlanta.
Convert each of these three lists to a set.
Using a set operation, find the IP addresses that are duplicated between Houston and Atlanta.
Using set operations, find the IP addresses that are duplicated in all three sites.
Using set operations, find the IP addresses that are entirely unique in Chicago.
3. Read in the ‘show_version.txt’ file. From this file, use regular expressions to extract the OS version, serial number, and configuration register values.
Your output should look as follows:
OS Version: 15.4(2)T1 Serial Number: FTX0000038X Config Register: 0x2102
4. Using a named regular expression (?P<name>), extract the model from the below string:
show_version = ''' Cisco 881 (MPC8300) processor (revision 1.0) with 236544K/25600K bytes of memory. Processor board ID FTX0000038X 5 FastEthernet interfaces 1 Virtual Private Network (VPN) Module 256K bytes of non-volatile configuration memory. 126000K bytes of ATA CompactFlash (Read/Write) '''
Note that, in this example, ‘881’ is the relevant model. Your regular expression should not, however, include ‘881’ in its search pattern since this number changes across devices.
Using a named regular expression, also extract the ‘236544K/25600K’ memory string.
Once again, none of the actual digits of the memory on this device should be used in the regular expression search pattern.
Print both the model number and the memory string to the screen.
5. Read the ‘show_ipv6_intf.txt’ file.
From this file, use Python regular expressions to extract the two IPv6 addresses.
The two relevant IPv6 addresses you need to extract are:
Try to use re.DOTALL flag as part of your search. Your search pattern should not include any of the literal characters in the IPv6 address.
From this, create a list of IPv6 addresses that includes only the above two addresses.
Print this list to the screen.