Lab 3 of 5
Β·
Semi-Guided
Generate Switch Config with string.Template
Objective: Use string.Template to fill a switchport config block from a list of port records.
Instructions
Create a file called lab3.py. Use string.Template to generate a config block for each port in the list below.
from string import Template
port_template = Template(
"interface $interface\n"
" description $description\n"
" switchport access vlan $vlan\n"
" switchport mode access"
)
ports = [
{"interface": "GigabitEthernet0/1", "description": "PC-Finance-01", "vlan": 10},
{"interface": "GigabitEthernet0/2", "description": "PC-Helpdesk-01", "vlan": 20},
{"interface": "GigabitEthernet0/3", "description": "IP-Phone-01", "vlan": 30},
]
Print each config block separated by a blank line.
Your output must match exactly:
interface GigabitEthernet0/1
description PC-Finance-01
switchport access vlan 10
switchport mode access
interface GigabitEthernet0/2
description PC-Helpdesk-01
switchport access vlan 20
switchport mode access
interface GigabitEthernet0/3
description IP-Phone-01
switchport access vlan 30
switchport mode access
Run it with python3 lab3.py. Paste your output below and submit.
π‘ Show Hint
Use port_template.substitute(**port) to unpack the dict as keyword arguments. Use print() after each block and an extra print() to produce the blank line separator β or join with "\n\n".join(...).
Paste Your Output