Okay.
So I'm going to copy this bit of Python code.
Go back to the Nexus box.
At the moment I'm not running Python.
I can run standard Cisco iOS commands such as show IP interface brief.
All show version.
So these are standard commands, show, run, etc. But what I'm going to do now is run Python.
This is on box python.
I'm running Python directly on this Nexus box.
And I'm going to teach you about a python now.
The first statement we're going to use is from Sealy import star.
So from cli import, store or import everything.
To be able to use cli commands from within python running on this nexus box, we need to import cli.
We need to talk to that command to allow us to basically run any CLI command.
This is called a variable CMD one.
You could call this anything you wanted to.
The command that we're going to run is show IP interface, brief JSON pretty.
So we basically using the command that I previously demonstrated.
So I'll quit out of Python just to show you that again.
Show IP interface brief.
Jason.
Pretty.
That's me typing the command manually.
And here's our output once again, json array.
But I'm going to run python on box.
The version of Python.
He has 2.7 3011.
Now for the real world.
You really want to learn Python three if you're new to Python.
But here's a real world example of why sometimes it's well worth knowing about Python 2.7 because you'll
still encounter it.
Devices like this nexus are running Python 2.7, not Python three.
So once again, I need to type from CLI, import everything.
And then I'm going to paste this statement that says this command is going to be stored in this variable.
So if I type CMD one.
That's the command that's stored in this variable.
I explained Python theory a little bit later in the course.
If you want to learn a bit about Python theory, such as the fact that a variable is essentially a placeholder
in memory on the device, then continue watching again.
You don't need to learn Python for the exam.
CCNA exam.
Definite exam you would need to.
So I'm going to store the output of that command in another variable.
Basically, the output variable stores, the output of this command, which I'm running using the CLI
function.
So if I look at output one now, there's the output.
There's the information stored in that variable.
But it's not very easy to read some of the type print output one.
And there you go.
There's the output of that show command in JSON formatting running within Python.
Now if you're new to Python, you might say, but David, I could just type the command manually.
Sure you could.
But I'm showing you this step by step.
In a moment, I'm going to show you how to run a Python script from my local computer that searches
into this device and runs one or two commands and then extracts information from that device and shows
it on my local computer.
So rather than me having to manually SSH log into the device type of command, see the output and then
interpret it, I can get a Python script to automatically do that.
In this example, I'm doing it for one device, but remember, I could do this with 100 devices or 500
devices.
Automation and network programmability make a lot of sense when you have many devices.
Not necessarily always when you only have one device, but you need to work in building blocks.
So basically this little script of four lines and I could have made it short if I wanted to, but I
don't want to make it complex.
I want to make it easy to read.
Short script of four lines has given me the output of show IP interface brief using JSON pretty format.
What we can do now is extract specific information from the table, interface dictionary and the row
interface dictionary.
This is what's called a dictionary.
We can query this information.
So as an example, I could query the IP address of a specific interface if I wanted to.
So let's do that by updating our script.
So this information is the same, but what I'm going to do now is load that information using this command
so that Python knows that this information is in JSON format at the moment.
It's currently a string.
So if I use the type command and look at output one, it's a string and we can't query that data format
as easily as JSON.
So it's going to make much more sense to change this into a dictionary using JSON formatting so that
we can query the information much more easily.
Remember, the capture.
Capture is a way to stop machines.
It's easy for humans, but not easy for machines.
Here we want to put it in a format that machines understand.
And again, if you know Python, you could do this more simply than I am.
It's complaining about my script, and that's because I forgot this command.
Previously I had this as output one and I actually changed the script.
So that's bad.
So let's do that again.
So to make sure that you're not confused, I'll simply start this from the beginning again.
So I'll quit out of Python and let's start again.
So Python 2.7, these commands you've already seen.
So let's copy those commands and I'll paste them into Python, which is running on that Nexus device.
This statement runs this command using the CLI function and stores it in JSON data rather than in output
one, which is the name I used previously.
So that JSON data now contains the information.
But the problem with that is it's a string, so notice type string.
So what I want to do is I want to load that data using JSON loads.
Some are loading this information using this function and I'm storing it in this variable now JSON final.
Has that information.
I actually wanted to use type here so that we can see what type of this is.
What you can see is it's now a dictionary.
Dictionaries in Python use key value pairs.
So I can query very easily within the dictionary for specific piece of information rather than trying
to pass or go through a string and look for specific characters and then find the answer.
That's very difficult to do for a machine.
Very easy for us as humans.
But once again, it's the idea of show version.
It's just text.
Easy for a human to read that, not for a machine.
If I put that into JSON format, it's much easier to find information.
So as an example, what I can do now is print the prefix of the first interface.
So going up through this information, not easy to reach here, but notice the first interface is VLAN
100.
First IP address is this.
So if I paste that command in.
We've just retrieved the IP address or prefix of the first interface.
Again, that's not very easy, but don't lose sight of the fact that I'm going to show you a script
in a moment that automates all of this.
Just trying to show you step by step.
So again, just to make sure that you understand the script, we are importing clay so we can use CLI
commands within Python.
We importing JSON so that we can use this command which allows us to take the string data and basically
convert it to a dictionary.
So we have JSON formatting.
This is the command that we ran on the switch.
Show IP interface, brief JSON pretty.
And then I've now queried that data, which is stored in what's called a dictionary.
A dictionary in Python is an unordered key value pair, so I can simply look for a value in the dictionary
and easily retrieve a piece of information.
So again, I've retrieved the IP address of the first interface starting at zero.
Now if I want the IP address of the second interface, I could change that to one third interface.
Fourth interface.
Now I'm doing that manually.
That's not the idea with automation.
The idea with automation is to automate this.
But I'm building or iterating is the term we use in programming.
I'm iterating the process, improving what I'm doing, and hopefully teaching you along the way.
Again, you don't have to learn Python for the CCNA exam, but you need a node for the dev net exam.
So if you're struggling with this stuff, don't worry too much.
But notice how cool this is.
We are running Python on a Cisco Nexus device, so I simply started Python on the Nexus device.
Once again, I'll exit out of Python.
I simply typed Python to start Python on the Nexus device and now I'm running Python scripts directly
on the Nexus device.
I'm going to show you some other videos in this course showing you how to do sort of a Linux based commands
and Python on XY.
This is Nexus.
iOS X also supports Python, so you can run Python directly on an iOS X device.
Classic or monolithic.
iOS doesn't support Python, but you can still use Linux commands on classic iOS.
Notice where the world is going.
Notice how it's important for you to learn Python Network Automation, Linux.
These kind of new skills is where the industry is moving.