Okay.
So this is just for us as humans.
Show IP interface brief doesn't really help us.
What I'm going to do now, however, is use the command json pretty.
So this line in yellow is actually the only change in the script.
But I've put the full script in this PowerPoint presentation so that you can download it.
So let's use Nano.
Nano once again.
Nano is a simple text editor.
Similar to Notepad on Windows, if you like.
I want to say Nano Python two and what I'll do is paste my script in here.
He had pasted it correctly.
So I'll save that script.
Only difference once again is this.
So Python three, run the second script.
This should give us our JSON information, so we should be able to get the JSON information back from
the Nexus device.
And there you go.
So.
Scrolling up.
By using that single command, I've now been able to S-H to the device, retrieve the data.
It's in JSON format.
Notice double inverted commas.
We've got a colon.
We've got a comma at the end.
Make sure you recognize this formatting.
I've said it once again many times.
If you don't have a colon there, there would be an incorrect answer.
If this is a semicolon, that would be an incorrect answer.
It has to look like this.
No comma at the end here.
Okay.
But once again, that's just given us some data.
Perhaps not that useful, but let's do something useful with the data now and let's get the prefix from
the data and the interface.
So basically I'm using a script now to pull back the first interface and the first IP address.
The worry in the next script, I'm going to show you how to do that from multiple interfaces.
So now, Liz, we've got two Python scripts.
Let's create a third Python script.
So Nano Python three dot p y.
I'll paste the script in the stuff that we've added.
Is this so we pulling back specific interface information now I have made done something.
Badshah I'm using double inverted commas and single inverted commas in Python.
You should standardize on one.
I'll fix that in the script that I give you, but that's fine.
Python will accept that even though it's not great practice.
But hopefully what will happen now is this will connect to that Nexus device once again searching across
the Internet and it's going to pull back.
First interface, first IP address and then display that and there we go.
Okay.
So just to prove it, here's my Nexus device once again, first interface, first IP address.
There's that information.
Now, you might say, okay, but I want the state information.
So something like this.
And remember, we can simply edit that.
Using our script.
So let's edit the script.
So rather than just seeing this data.
What we said is we want to get protocol, state information, so we'll copy that line.
Paste.
And what we want is proto state is the name that they use and I'll save the script, so run it again.
So we should get the protocol status.
We should get the interface name, we should get the IP address.
This, however, will only be for one interface.
Next script.
I'll change that and make it better.
Now the formatting here is not great.
I mean, that's just saying up.
You could print out information.
This is the whole idea with iteration.
So rather than just saying that, you could say print interface.
And while I'm here, let's fix this.
I'd have to fix that for all the data to be consistent.
I could do a fine and replace.
I suppose they would actually make a lot more sense.
Then doing it manually.
But the idea in Python is something called Pep eight.
Pep eight is talking about best practices.
The moral of the story is be consistent so we could say print.
IP address.
Print.
Protocol status.
These are just examples.
You could do what you wanted here, and this is the power of programming.
You just change it the way that you want it to change.
So it's got a problem.
And this is my python to history getting to me in Python three.
We need to use a function for print print as a function.
So this needs to be in brackets.
Save that.
Run it again.
Hopefully this once again will connect to that Nexus device and will tell us IP address, interface
and protocol status and also showed in a little bit nicer format.
So here we go.
There's the output of that script.
Now you might say, okay, but I want the information for all interfaces.
So I've got the script across two PowerPoint slides here.
What I'm doing here is creating a loop.
So we are all using a for loop here.
What we're doing is getting the length of the rows.
So how many rows are there?
So if we jump back onto the device, so this is going directly to the device show IP interface brief
JSON pretty.
Row interface tells us how many interfaces they are one, two, three, four, etc..
Show IP interface brief would just show us that.
So here are all the interfaces.
This length command allows me to work out how many interfaces they are, and then it just loops those
commands for the number of interfaces that we've got.
Okay.
So first thing is.
Knit Mico configuration.
So Liz, we are now working on a Python script for a Python forward API.
I'll paste that in that basically once again just allows me to connect to the device.
Didn't have enough space in PowerPoint.
I'll copy this into the VM now.
So the big difference now is it's using the command and then finding out how many interfaces there are
and then using a loop to basically run these commands multiple times for the number of interfaces that
we have.
So clear the screen Python three, Python four dot p y.
This should hopefully now give us IP address an interface name for all the interfaces on that device.
And to prove it, I'll create some extra interfaces and run the script again and we should see that.
So there you go.
All the interfaces on the device and the IP addresses.
So if I show that side by side, there are all the interfaces on the device and the IP addresses.
But let's create another loopback.
So let's create a loopback to IP address.
Two, two, two, two, slash.
Three Mosque interface loop three IP address slash 32 mosque show IP interface brief.
I've just created two additional loopback interfaces.
So run that script again.
This is once again connecting to that device in dev net and pulling back the information.
And there you go.
Additional loopback interfaces are shown just to prove it once more.
Last interface loopback for IP address.
Quadruple for show IP interface brief new interfaces created run the script again.
We didn't previously have loopback for in the output here, but hopefully we will now.
And there we go.
So why are we using JSON?
Much easier to work with this kind of data when you've got a Python script.
It's very difficult to find information if it's a string, if it's just data that's in an formatted
format.
So if it's just some kind of data that's like this capture, not easy for a machine to read that we
want to put it in a format that's easy for a machine to query and interpret.
Your Python scripts are going to be so much easier if the data is in JSON.
Trust me.
Try and do that kind of stuff with just strings and parsing the string, trying to find where the data
is and extracting a piece of data.
It's very hard to do that with traditional mechanisms, so much easier if the data is correctly formatted
using JSON or even XML or YAML.
Big idea for doing this is to make it easy for machines to read.
Okay.
So hopefully you've learned something.
I went to quite a bit of detail there with regards to the Python scripts.
You don't have to learn Python for the exam, but for the real world, I'd highly recommend it.