December 2018
Intermediate to advanced
226 pages
4h 57m
English
Let us examine a basic use of regex by parsing a base configuration containing the interface configuration of a router. The task is to identify the interfaces that have trunk enabled:
import resampletext="""interface fa0/1switchport mode trunkno shutinterface fa0/0no shutinterface fa1/0switchport mode trunkno shutinterface fa2/0shutinterface fa2/1switchport mode trunkno shutinterface te3/1switchport mode trunkshut"""sampletext=sampletext.split("interface")#check for interfaces that are in trunk modefor chunk in sampletext: if ("mode trunk" in chunk): intname=re.search("(fa|te)\d+/\d+",chunk) print ("Trunk enabled on "+intname.group(0))
The output for the preceding code is given as the following:
Here, ...
Read now
Unlock full access