A network has several switches and each switch has its own dictionary.
You can put all these dictionaries in a list:
Figure 1 - A List That Holds Two Dictionaries
In the lab, add the switch SW3 with the address 10.0.0.4 to the list
inventoryto get the flag.Answer the question below
Enter the flag you obtained.
To read a value in the list
inventory, you write the index of the switch, then the key.
You read the line from left to right:![In inventory[1]["ip"], [1] takes the item 1 of the list, then ["ip"] takes the key ip of this item, which gives "10.0.0.3".](https://dphfzu3i54bzu.cloudfront.net/media/10326/python-nested-access-path.png)
Figure 2 - First the Index, Then the Key
Here you read the name of the first switch:
print(inventory[0]["name"]) # Output: SW1In the lab, print the address of the last switch of the list
inventoryto get the flag.Answer the question below
Enter the flag you obtained.
The value of a key can also be a list.
Here the dictionary of a switch holds the list of its ports:![In the dictionary switch, the key "name" holds "SW1" and the key "ports" holds the list ["Gi0/1", "Gi0/2"], the ports of this switch.](https://dphfzu3i54bzu.cloudfront.net/media/10288/python-list-inside-dictionary.png)
Figure 3 - A List as the Value of a Key
You read the list with its key, then one item with its index:
switch = { "name": "SW1", "ports": ["Gi0/1", "Gi0/2"] } print(switch["ports"][0]) # Output: Gi0/1In the lab, print the last port of the switch to get the flag.
Answer the question below
Enter the flag you obtained.
To work on every port of every switch, you put a loop inside a loop.
The first loop takes each switch and the second loop takes each port of this switch:![The first line, for switch in inventory, runs for each switch of the inventory, and the second line, for port in switch["ports"], runs for each port of this switch.](https://dphfzu3i54bzu.cloudfront.net/media/10289/python-nested-loops.png)
Figure 4 - A Loop Inside a Loop
With two switches of two ports each, the
printline runs four times.In the lab, print the name of each switch.
Under each name, print the commandinterfacefor each of its ports to get the flag.Answer the question below
Enter the flag you obtained.