Skip to content

Challenges

Hints

Recap what you have learned about Lists and Strings so far. Here are some tips to help you get started:

  • You can use the append() method to add items to a list.
  • You can use the split() method to split a string into a list.
  • You can use f-strings to format strings.

W3Schools has a nice library of examples of string methods - check out the join() and split() methods.

Also check the f-strings documentation.

Challenge 1

You been given a list of RFC1918 networks and a destination IP address and port.

rfc1918 = """
10.0.0.0,0.255.255.255,permit
172.16.0.0,0.0.15.255,permit
192.168.0.0,0.0.255.255,permit
"""

destination = "192.168.22.12,443,tcp"

Make a list of str of valid access-list entries.

Example of the output:

[
    "access-list 100 permit tcp 10.0.0.0 0.255.255.255 host 192.168.22.12 eq 443",
    "access-list 100 permit tcp 172.16.0.0 0.0.15.255 host 192.168.22.12 eq 443",
    "access-list 100 permit tcp 192.168.0.0 0.0.255.255 host 192.168.22.12 eq 443",
]
Solution

Here is one way to solve this challenge:

rfc1918 = """
10.0.0.0,0.255.255.255,permit
172.16.0.0,0.0.15.255,permit
192.168.0.0,0.0.255.255,permit
"""

destination = "192.168.22.12,443,tcp"

# Split RFC1918 networks
lines = rfc1918.strip().split("\n")
A = lines[0].split(",")
B = lines[1].split(",")
C = lines[2].split(",")

# Split destination
dest_ip, dest_port, protocol = destination.split(",")

# Build ACE list
ace_list = [
    f"access-list 100 {A[2]} {protocol} {A[0]} {A[1]} host {dest_ip} eq {dest_port}",
    f"access-list 100 {B[2]} {protocol} {B[0]} {B[1]} host {dest_ip} eq {dest_port}",
    f"access-list 100 {C[2]} {protocol} {C[0]} {C[1]} host {dest_ip} eq {dest_port}",
]

print(ace_list)

Challenge 2

Given the output from challenge 1, use the join() method to create a configuration multiline string. Ask the user to input the number of the access-list and use the replace() method to update the access-list number in each entry.

Solution

Here is one way to solve this challenge:

# Assume ace_list is the output from Challenge 1
ace_list = [
    "access-list 100 permit tcp 10.0.0.0 0.255.255.255 host 192.168.22.12 eq 443",
    "access-list 100 permit tcp 172.16.0.0 0.0.15.255 host 192.168.22.12 eq 443",
    "access-list 100 permit tcp 192.168.0.0 0.0.255.255 host 192.168.22.12 eq 443",
]

# Ask user for ACL name
acl_name = input("Enter the name of the access-list: ")

# Replace ACL number with the user-provided name
line_one = ace_list[0].replace("100", acl_name)
line_two = ace_list[1].replace("100", acl_name)
line_three = ace_list[2].replace("100", acl_name)

# Join into a multiline configuration string
config = "\n".join([line_one, line_two, line_three])

print("\nGenerated Configuration:\n")
print(config)