Hello
This time I tried out some ready-made templates that some folks prepare to process cisco show command outputs:
from ntc_templates.parse import parse_output
vlan_output = (
„VLAN Name Status Ports\n”
„—- ——————————– ——— ——————————-\n”
„1 default active Gi0/1\n”
„10 Management active \n”
„50 VLan50 active Fa0/1, Fa0/2, Fa0/3, Fa0/4, Fa0/5,\n”
” Fa0/6, Fa0/7, Fa0/8\n”
)
vlan_parsed = parse_output(platform=”cisco_ios”, command=”show vlan”, data=vlan_output)
!!!the output gave us a list of dictionaries that we need to loop through.
In the first step we see that length of vlan_parsed is 3, so we can loop through that.
When we are inside the first loop, we have dictionary objects, so we can use the .items function to go through that, just remembering that because we are inside of the first loop we need to call this by list[x].
x = 0 while x <len(vlan_parsed): print() print() print('NEXT VLAN !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!') print() for key, value in vlan_parsed[x].items(): print('---------------------------------') print(key, ' : ', value) x += 1
And voila!
NEXT VLAN !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
———————————
status : active
———————————
name : default
———————————
vlan_id : 1
———————————
interfaces : [‚Gi0/1’]
NEXT VLAN !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
———————————
status : active
———————————
name : Management
———————————
vlan_id : 10
———————————
interfaces : []
NEXT VLAN !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
And yes, i know it’s still screen scraping. But kind of easier because you don’t have to do the regex and string manipulation yourself.
Next example was a bit more difficult because some templates don’t allow for headers. If something doesn’t work, look in your /home/.local/lib/python3.5/site-packages/ntc_templates/templates
In this case after „start’ we have just a record line, so in my show ip interface brief i removed the headers and just pasted the output.
cat cisco_ios_show_ip_interface_brief.textfsm Value INTF (\S+) Value IPADDR (\S+) Value STATUS (up|down|administratively down) Value PROTO (up|down) Start ^${INTF}\s+${IPADDR}\s+\w+\s+\w+\s+${STATUS}\s+${PROTO} -> Record # Capture time-stamp if vty line has command time-stamping turned on ^Load\s+for\s+ ^Time\s+source\s+is
So here’s my final python script
templates.parse import parse_output ship_output = ( "GigabitEthernet0/0/0 15.10.8.107 YES NVRAM up up\n" "Loopback1255 111.21.186.11 YES NVRAM up up\n" "Tunnel1251 111.21.173.11 YES NVRAM up up\n" "Tunnel1255 111.21.187.11 YES NVRAM up up\n" "Vlan1 192.168.8.11 YES NVRAM up up\n" ) ship_parsed = parse_output(platform="cisco_ios", command="show ip interface brief", data=ship_output) x = 0 while x <len(ship_parsed): print() print() print('NEXT ONE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!') print() for key, value in ship_parsed[x].items(): print('---------------------------------') print(key, ' : ', value) x += 1
and the script output is…
NEXT ONE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! --------------------------------- proto : up --------------------------------- status : up --------------------------------- intf : GigabitEthernet0/0/0 --------------------------------- ipaddr : 15.10.8.107 NEXT ONE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! --------------------------------- proto : up --------------------------------- status : up --------------------------------- intf : Loopback1255 --------------------------------- ipaddr : 111.21.186.11 NEXT ONE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! --------------------------------- proto : up --------------------------------- status : up --------------------------------- intf : Tunnel1251 --------------------------------- ipaddr : 111.21.173.11 NEXT ONE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! --------------------------------- proto : up --------------------------------- status : up --------------------------------- intf : Tunnel1255 --------------------------------- ipaddr : 111.21.187.11 NEXT ONE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! --------------------------------- proto : up --------------------------------- status : up --------------------------------- intf : Vlan1 --------------------------------- ipaddr : 192.168.8.11 >>> >>>