Now so far we have been able to send show commands to our target devices.
Now whilst this is useful for you as a human engineer to actually be able to
see the
output of these commands, quite honestly this is not the best data to work with
programmatically
quite simply because the information comes back just as a regular string blob.
Now we know with respect to Python it's much better to work with this type of
structure
data whereby we have access to these Python objects.
Now the cool thing is with NetMico there actually is integration with something WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Now the cool thing is with NetMico there actually is integration with something
known as
text FSM.
So what has happened is that the network automation community online has
utilized this
particular utility known as text FSM and generated particular templates that
actually transfer
or rather transform should I say transform that regular string like data into
structure
data that we can easily parse.
Let me show you what I mean. If you just happen to search for text FSM
templates and click
on this link here within the network to code get up. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
on this link here within the network to code get up.
See if you just scroll on down and go into this folder here for NTC templates
and then
templates once again.
What you're going to see are tons and tons and tons of these text files, some
for
Arista, some for Junotware, some for Cisco, so on and so forth.
They're pretty much if you scroll on down to Cisco iOS, like right here, what
we're actually
seeing here is all of the available show commands that these templates have
been configured
to translate the string data into these Python objects.
So say for example, we wanted to go and send a show command for SNMP. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
So say for example, we wanted to go and send a show command for SNMP.
Let's check this here.
So scrolling on down we have the command show SNMP community.
If we open this up, what we see here is this funny looking information.
Now this looked pretty confusing and the good thing is we don't have to care
anything
about it.
We're not trying to write it.
We just want to use it.
So here's the thing.
NetMico makes it really quite easy.
So if I just copy over example five and I create a new one called example seven
dot
py, let's open this up.
Okay.
So check this out. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
So check this out.
All we're doing is what we saw before.
We're going to open up our file right here, look through all of our devices and
send
the show command.
Now the show command we're sending here is show IP interface brief.
Let's actually change this to be show, rather than snow show SNMP community.
Okay, save this.
So let's quickly just try the actual script right here and to enter and look at
this right
here.
We get the output for the first device as well as a second, third and then at
the bottom
the fourth device.
Now right now we can't just call these dictionary keys to return particular WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Now right now we can't just call these dictionary keys to return particular
pieces of data
because it is all just regular string data.
However, if we just use this simple option by saying comma and then say use
text FSM and
specify the value is true.
If I just save this and run the script again and to enter now we actually see
here the
information is coming back as structured data.
And if I just to make it look a little bit more readable, what I will just say
is pit
three install rich.
This is just a library to make things prettier to look at.
And what I will say here is from rich import print as our prints. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
And what I will say here is from rich import print as our prints.
And as opposed to printing the result, I will our print results.
Okay, let's try this again.
So now this looks a little bit easier to look at.
Okay.
So what we can actually see here is the first element is actually a list
because we see
your square braces and then inside that list there is one object, a dictionary.
And then we have access to your keys to pretty much if we call list index zero,
this will
strip away the outer list.
So print results and then square brackets zero.
Try it again.
Arow up.
Look at this right here.
We're now getting back a dictionary for each of our devices. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
We're now getting back a dictionary for each of our devices.
So say for example, I want to grab the name key.
What I can now do is easily programmatically access this.
So check this out.
Grab the name key.
Arow up.
Try again.
Second, third and the fourth test community is coming through.
Now just for posterity, let me just happen to change this on one of the devices
.
Let's need to go to device number three here.
Let's open this up.
Print enable mode show run section SNMP.
Now if I just say configure terminal and I say no SNMP server community test WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Now if I just say configure terminal and I say no SNMP server community test
community
are all so basically I've removed it.
And what I will now do is just simply creating new SNMP configuration on this
particular
device.
Let's call this one random community.
Okay.
So it's not going to be the same as the others.
So let's just try to run this.
Here's device one device to device three is different and device four is still
the same.
So because we now have access to this data programmatically, we can ultimately
use conditional
logic to run tests against their network.
So what I can do is as opposed to just printing this data, I can just say if WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
So what I can do is as opposed to just printing this data, I can just say if
this particular
object that we're parsing out, which is ultimately the name key, if that equals
the value
of test community, what I can do is print out this device past the test for SN
MP.
However, if it doesn't have that value of print outs, this device has failed
the test
for SNMP.
So now because I have this structured data, I can access very precise details
programmatically
and now run tests against the state of my network.
So let's try this here. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
So let's try this here.
First device pass, second device passes, third device fails the test and the
fourth device
passes the test.
So quite simply, I see, oh, device number three has a problem.
What am I going to do?
Well, go and fix it.
Let's go and just say no for the random community and what I will do is I'll
put the correct
configuration on of test community to now it should be fixed.
If I rerun my little test and to enter device one and two passes expected
device three also
passes now as well as device number four.
So just imagine the possibilities now all of your show commands, you can access WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
So just imagine the possibilities now all of your show commands, you can access
the data
programmatically and now use Python to write very particular logic to test the
state of all
of your devices no matter how many devices you have.
Say for example, you've got 10 devices, 100 devices, 1000 devices, it does not
matter.
You can just sit back with your coffee, run your script and let Python find out
if your
network is actually correctly configured.