Monday, January 2, 2017

So I'll explain..SCADA systems, or multicast configurations on CISCO routers, have not more by default ways to go over and over routing packets...but I want more than that solution..what I want is to READ all vrf ip PID's...but more important of course than reaching to PLC's circuits, is to have total control on the OBJECT_TYPE...meaning the logger keys.

eAPI Python script to look at ARP entries per VRF

I needed to see all the different ARP entries in each VRF, so I wrote up this little script to do just that. The ‘show vrf’ command in eAPI has not yet been converted to JSON, so I had to do some text parsing to get the VRF names, then use those names to grab the ARP entries. On line 4 you’ll see that I use the ‘text’ option for the output of the JSON reply. That allows me to run a command that hasn’t been converted yet and get the raw text output:
response = switch.runCmds( 1, ["show vrf"], "text" )
The output looks like this:
"output": "   Vrf         RD            Protocols       State         Interfaces \n----------- ------------- --------------- ---------------- ---------- \n   test        100:100       ipv4            no routing               \n   test2       101:101       ipv4            no routing               \n   test3       102:102       ipv4            no routing               \n\n"
Or in a more familiar format:
   Vrf         RD            Protocols       State         Interfaces
----------- ------------- --------------- ---------------- ----------
   test        100:100       ipv4            no routing
   test2       101:101       ipv4            no routing
   test3       102:102       ipv4            no routing
Then I take the output and use splitlines() to take each line (separated by newline) and insert them into a list:
lines = response[0]['output'].splitlines()
Now I iterate through each entry of the ‘show ip vrf’ output and issue a ‘show ip arp vrf’ with the VRF name. I use the range() function, starting at the 3rd line (since the first two are just header lines), and go through the end of the list. Then I use the split() method to split each line on whitespace, taking the first entry which corresponds to the VRF name. Finally, I can use that VRF name in my command.
for i in range(2, len(lines) - 1):
  vrfname = lines[i].split()[0]
  command = "show ip arp vrf " + vrfname
Here’s the script in its entirety:
from jsonrpclib import Server
switch = Server( "https://admin:admin@leaf1/command-api" )
response = switch.runCmds( 1, ["show vrf"], "text" )
lines = response[0]['output'].splitlines()
for line in lines:
print line
for i in range(2, len(lines) - 1):
vrfname = lines[i].split()[0]
command = "show ip arp vrf " + vrfname
response = switch.runCmds( 1, [command] )
print vrfname
print response[0]

No comments:

Man in the Rain