When I was creating the page in my Scrapli and Flask Project for Multicast details. I had a requirement to split a string on multiple delimiters. As always Stack Overflow came to aid me.
The “show ip mroute” plain text output from the Nexus switch uses multiple delimiters for new lines, three specifically: “\n, \n\n, \n\n\n”.
I have an output of the dictionary I originally created that contained the output in a string format showing the new line characters.
0 1 2 |
{'mcast_nexus4': 'IP Multicast Routing Table for VRF "default"\n\n(*, 232.0.0.0/8), uptime: 4d23h, pim ip\n Incoming interface: Null, RPF nbr: 0.0.0.0\n Outgoing interface list: (count: 0)\n\n\n(*, 239.0.1.2/32), uptime: 1d23h, igmp ip pim\n Incoming interface: Ethernet1/2, RPF nbr: 10.1.2.1\n Outgoing interface list: (count: 1)\n Vlan50, uptime: 1d23h, igmp\n\n\n(172.16.2.200/32, 239.0.1.2/32), uptime: 1d23h, ip mrib pim\n Incoming interface: Vlan16, RPF nbr: 10.1.6.1\n Outgoing interface list: (count: 1)\n Vlan50, uptime: 1d23h, mrib\n\n\n(*, 239.255.255.250/32), uptime: 3d07h, igmp ip pim\n Incoming interface: Ethernet1/2, RPF nbr: 10.1.2.1\n Outgoing interface list: (count: 1)\n Vlan50, uptime: 3d07h, igmp', 'mcast_nexus3': 'IP Multicast Routing Table for VRF "default"\n\n(*, 232.0.0.0/8), uptime: 22:43:51, pim ip\n Incoming interface: Null, RPF nbr: 0.0.0.0\n Outgoing interface list: (count: 0)', 'mcast_nexus2': 'IP Multicast Routing Table for VRF "default"\n\n(*, 232.0.0.0/8), uptime: 4d23h, pim ip\n Incoming interface: Null, RPF nbr: 0.0.0.0\n Outgoing interface list: (count: 0)\n\n\n(*, 239.0.1.2/32), uptime: 1d23h, pim ip\n Incoming interface: loopback0, RPF nbr: 10.1.10.1\n Outgoing interface list: (count: 1)\n Ethernet1/2, uptime: 1d23h, pim\n\n\n(172.16.2.200/32, 239.0.1.2/32), uptime: 1d23h, pim ip\n Incoming interface: Ethernet1/1, RPF nbr: 10.1.1.1, internal\n Outgoing interface list: (count: 0)\n\n\n(*, 239.255.255.250/32), uptime: 3d07h, pim ip\n Incoming interface: loopback0, RPF nbr: 10.1.10.1\n Outgoing interface list: (count: 2)\n Ethernet1/2, uptime: 3d07h, pim\n Ethernet1/1, uptime: 3d07h, pim', 'mcast_nexus1': 'IP Multicast Routing Table for VRF "default"\n\n(*, 232.0.0.0/8), uptime: 4d23h, pim ip\n Incoming interface: Null, RPF nbr: 0.0.0.0\n Outgoing interface list: (count: 0)\n\n\n(172.16.2.200/32, 239.0.1.2/32), uptime: 1d23h, ip pim\n Incoming interface: Vlan20, RPF nbr: 172.16.2.200\n Outgoing interface list: (count: 1)\n Vlan16, uptime: 1d00h, pim\n\n\n(*, 239.255.255.250/32), uptime: 3d07h, igmp ip pim\n Incoming interface: Ethernet1/1, RPF nbr: 10.1.1.2\n Outgoing interface list: (count: 1)\n Vlan20, uptime: 3d07h, igmp'} |
This is how it should look. Very readable as is from the Cisco command line.
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
IP Multicast Routing Table for VRF "default" (*, 232.0.0.0/8), uptime: 4d23h, pim ip Incoming interface: Null, RPF nbr: 0.0.0.0 Outgoing interface list: (count: 0) (*, 239.0.1.2/32), uptime: 1d22h, igmp ip pim Incoming interface: Ethernet1/2, RPF nbr: 10.1.2.1 Outgoing interface list: (count: 1) Vlan50, uptime: 1d22h, igmp (172.16.2.200/32, 239.0.1.2/32), uptime: 1d22h, ip mrib pim Incoming interface: Vlan16, RPF nbr: 10.1.6.1 Outgoing interface list: (count: 1) Vlan50, uptime: 1d22h, mrib (*, 239.255.255.250/32), uptime: 3d07h, igmp ip pim Incoming interface: Ethernet1/2, RPF nbr: 10.1.2.1 Outgoing interface list: (count: 1) Vlan50, uptime: 3d07h, igmp |
The Fix
For a quick fix, see the Stack Overflow. For my case I used the same, but adapted it when creating my dictionary. This worked perfectly, below is an excerpt. For the full script this can be seen in my Scrapli and Flask Project.
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import re show_output_dict = {} parsed_output = [] for device in genie_list: for key, value in device.items(): if key != "show_output": print("\n") print(f"Hostname: {key}") show_output_dict.update({key:re.split('\n\n|\n|\n\n\n',device['show_output'])}) parsed_output.append({key:value}) |