I am trying to parse through show commands using regex in show interface commands in a router.
The output will be something similar to this:
Ethernet0/0 10.10.10.103 YES DHCP up up
GigabitEthernet0/1 unassigned YES manual administratively down down
Ethernet0/2 unassigned YES manual administratively down down
Ethernet0/3 unassigned YES manual administratively down down
How can I grab the interface descriptors (i.e. GigabitEthernet0/1 ) without any other data?
Is there a way to grab the first string on each line up to the first space (" ")? These outputs will have 100 or more interfaces with many combinations of letters and numbers.So this will help if I can find a solution with Regex.
tried this in python- interface_pattern = re.findall(r'\w+*\w*|\w**\w+', output)
within python but returned none
We can use re.findall
in multiline mode as follows:
inp = """Ethernet0/0 10.10.10.103 YES DHCP up up
GigabitEthernet0/1 unassigned YES manual administratively down down
Ethernet0/2 unassigned YES manual administratively down down
Ethernet0/3 unassigned YES manual administratively down down"""
matches = re.findall(r'^\S+', inp, flags=re.M)
print(matches)
# ['Ethernet0/0', 'GigabitEthernet0/1', 'Ethernet0/2', 'Ethernet0/3']