- Practical Network Automation
- Abhishek Ratan
- 176字
- 2021-07-02 14:53:08
Making the right choice
In this example, we will use a switch case to identify the right set of configurations based upon certain input given by the user.
As a prerequisite understanding, the syntax of the exec-timeout command based upon OS is as follows:
- Cisco IOS command: exec-timeout 15 0
- Cisco NXOS command: exec-timeout 15
#create a dictionary:
config={
"IOS":"exec-timeout 15 0",
"NXOS":"exec-timeout 15"
}
getchoice=input("Enter IOS type (IOS/NXOS) : ")
if (getchoice == "IOS"):
print (config.get("IOS"))
if (getchoice == "NXOS"):
print (config.get("NXOS"))
The sample output is as follows:
>
Enter IOS type (IOS/NXOS) : IOS
exec-timeout 15 0
>>>
Enter IOS type (IOS/NXOS) : NXOS
exec-timeout 15
>>>
In the preceding example, we have tackled a common challenge of using a switch case in Python. Unlike some other languages, Python does not provide a switch case statement, hence we need to use a dictionary to overcome this. Using this approach, we can remove the usage of multiple if statements and directly call the dictionary values based upon the mappings done in the dictionary.