So friends, now that we know how to send get requests and Python and we can
ultimately work with the response in a programmatic manner,
let's now target a network device that has been enabled for risk-conf.
So one of the best utilities that you can use for automation and programm
ability for testing and learning is to go to the Cisco DevNet sandbox.
If I click this link here, make sure you have an account in login.
Now what I will do is I will launch the sandbox.
Now what I am going to do is I am going to search for Xe for Cisco iOS Xe and
this one here, at least when I am recording this right now, WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
this one here, at least when I am recording this right now,
this is the one that is available.
Cisco iOS Xe on the cat 8kV always on, always on means that we don't have to
reserve a spot.
So this is really quite handy.
So let's go and click this here to see the connection details.
So we can see here to actually connect to the device.
This is the public URL.
We have the login username, the password and we can see the risk-conf port is 4
443.
So let's go and see how we can actually make a request then to what it will do.
Let me just create a new directory here.
If I do not know what, let's keep it on the same directory that is not as the WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
If I do not know what, let's keep it on the same directory that is not as the
matter.
Let's create a script called script2.py.
So let's open this up.
So first things first, we are going to import the request library once again.
There is no surprise right there.
So let's go and define our headers in once again, new little dictionary.
So this time the cable still will be except because it is an except header.
But the corresponding value is a little bit different.
As opposed to simply being application for word slash JSON,
it's actually going to be application yang hyphen data plus JSON like so. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
it's actually going to be application yang hyphen data plus JSON like so.
This is because the data is going to be yang modeled and again formatted in
JSON.
This is what we're looking for from the server.
So let's now define our URL.
So let's go and grab the connection information.
So we can see here the public URL, at least when I am recording this.
At this time is this particular one right here.
Let's copy it.
And all I will do is I'll paste this in.
I'll put it in quotation marks right here.
And all I will do is I will add on at the start 80 to PS.
Go on forward slash forward slash. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Go on forward slash forward slash.
Now, crucially, we're going to be connecting over port 443 to default Rescon
port.
Now this part here might look a little bit strange.
So let's just follow this.
What I'm going to do here is I'm going to say forward slash Rescon.
Forward slash data forward slash I ETF dash interfaces.
Colon interfaces.
Let me just zoom out a little bit.
So as I mentioned in the opening, this part here is actually referenced in the
Yang Tree.
Now you may recall when we first talked about Yang in an earlier scale, we have WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Now you may recall when we first talked about Yang in an earlier scale, we have
three different main styles of Yang Models.
We have the I ETF Yang Models.
This is an open standard.
We also have open config Models.
Once again, this is also open and vendor neutral.
And then we also have the native Yang Models that are device or rather vendor
specific.
So Cisco specific Yang Models or Juniper specific Yang Models.
The one I'm using here is a vendor neutral one, meaning that this particular
request would work perfectly fine on a Cisco device. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
request would work perfectly fine on a Cisco device.
And it would also work perfectly fine on a Juniper device.
And more importantly, the way the information we come back would be structured
in the same style.
So that's all we're doing here.
In the upper specifying, we want a very particular resource. We want to see
interface information modeled in the I ETF interfaces Yang style.
And now what I will do is just pretty much as we saw before, I will create a
little variable to hold my response.
I will use the request library.
I'm going to have a get request.
And I'll just simply say the URL is equal to the URL I have to find above.
I'll see the headers are equal to the headers I have to find above. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
I'll see the headers are equal to the headers I have to find above.
But this part here is also a bit different.
The previous request that we did.
We didn't actually have to configure any type of authentication. We didn't have
to say a username a password that type of thing.
So what I will do here in this case is I'm going to pass in the auth keyword of
say auth is equal to.
And I'm going to make this what is known as the Python tuple.
So I'm going to have parentheses here.
And now I will just pass in the user name, which if we go to the sandbox, the
user name right here is admin.
As we can see, let's copy this. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
As we can see, let's copy this.
So paste this in.
And then I'll have a comma.
And now specify what the password is. So again, we go back, check the
credentials.
Again, just check this because like I say, they often happen to change.
So if you just blame the copy and paste from this particular skill, then well,
they might be out of date.
So just be aware of that.
So paste this in.
And now what I will do is just as we saw before, I will say verify is equal to
false.
And now we should pretty much have all the details that we need in order to
make our request.
So all I will do now is I will say prints, response and save you. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
So all I will do now is I will say prints, response and save you.
So let's give it a go.
We'll save Python 3 script 2.py before I actually run this.
Let's pull this up and now we'll hit enter.
And now look at this right here.
We're getting back this warning right here once again.
In fact, let me just disable that one more time.
Go up to the top.
Let's disable the warning.
I'll go up, try again.
So this time once again, we're getting our 200 response very much like with a
regular rest API.
The difference here though is this time we're now talking to a router.
So what I will do here is if I go back up.
And once again, I say I want to see the text response. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
And once again, I say I want to see the text response.
Let's give me the string based data.
Hit enter.
Look at it right here.
We're now seeing all of the Yang model data of this particular device.
So we can see here we have gigabit ethernet 1.
We have the name here.
It has a description on this interface configured by admin.
We can see the IPv4 address here.
See this right here.
And it's corresponding net mask.
And once again, this is all structured in JSON.
And once again, if I want to access this as Python objects,
like with send dictionaries, then I'll call the JSON method. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
like with send dictionaries, then I'll call the JSON method.
In fact, to make this look a little bit more readable, obviously from rich
import prints as our prints.
This will just give me some nice formatting and coloration to make things a
little easier to read.
I'll say pip3 install rich, perfect, and run this script again.
So we have a response.
And if I just scroll on up, what we can actually do now is just basically call
the key.
So this is the outer key right here.
So let's copy here.
And a similar way to what we just did.
Let's paste this in.
Save this.
Arow up. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Arow up.
Try again.
Now we strip away the outer key.
So I can grab the interface here.
Let's put the next key.
Paste it in.
And right after this, we're going to have a list object.
So what I can then do is call a particular index.
So let's maybe go with index position zero.
Let's try this and run this script.
Now we're getting much more specific information.
And again, if I want to get the name key for this particular interface,
I can then call it save you.
Arow up.
Try again.
And as we can see here, we're just targeting this very precise piece of data.
So now all of our configurations, and I mean all of our configurations are now WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
So now all of our configurations, and I mean all of our configurations are now
available as programmatic data.
Everything has been young model.
And like I say, this particular structure, the way I would parse this,
would be the exact same way on a jitter per device because we are using an open
vendor neutral young model, the IETF models.
So now we're talking programmatically to a device over 80 dps.
This all seems very, very good, but again, the mysterious part was how exactly
did I know to actually put this particular URL to get this very particular WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
did I know to actually put this particular URL to get this very particular
piece of information?
Well, once again, it's all about young.
Let's talk about that a little bit more in the very next video.