Now in the previous video, we saw how we could interact with the Cisco DNA
Center or Catalyst
Center always on Sandbox utilizing Postman.
What we're going to do now is translate the process that we did this time to
interact
with DNA Center with Python.
So with that said, let's just get to it.
So let's just create a little directory.
Let's just call this maybe say, D-N-A-C-D, enter this directory.
All I will do is I'll create a little script called script1.py.
Let's go and open it up. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Let's go and open it up.
All right, so PopQuiz, which library are we going to use in order to send HTTP
requests
with Python if you said the request library, well you're absolutely correct.
Let's go and do so.
Now a few things to actually specify.
The very first thing I will do is denote what my username and password is.
Remember, the first thing we're going to have to do is to use basic
authentication
with a post request to get that token.
So let's do that first thing.
Let me define my username.
And I'll just say the username is going to be DevNet user. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
And I'll just say the username is going to be DevNet user.
And the password is going to be Cisco 123 with an exclamation mark, OK?
Now let's define what the token URL is going to be.
So in my case here, I'm going to get the information of the Cisco always on
sandbox.
So let's go back to this page here, grab here, and I'll just paste it in.
And I will say forward slash.
And if you recall, the actual endpoint for the authentication,
as per the documentation, let's go to the quick start.
And the authentication, we can see it is this part here. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
And the authentication, we can see it is this part here.
So grab this.
All right, paste it in.
And let's get our game rid of this double forward slash.
There we go.
OK, so check this out.
What I'm going to do is to send my request.
So to say token response is equal to.
And all I will do is take my request library.
I'm going to send a post request, remember.
And I'm going to pass in some particular parameters here.
So also the URL is equal to the token URL I have defined above.
I'll specify the auth.
And I'm going to put this within parentheses because it's going to be a tuple. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
And I'm going to put this within parentheses because it's going to be a tuple.
And I'll specify I'm going to put in my username and password I have defined
above.
And I'll also specify that verification is going to be false because I don't
care
about these self-sounds certificates.
So right now, if I just happened to print out the token response and save this,
and let's go and run the script.
So I'll say Python 3 script 1.
And as we can see here, I do actually get a 200 response.
This indicates that we have a successful request,
but we actually want to get the information coming back.
So let's go and check this out. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
So let's go and check this out.
Then what I will do here is as opposed to printing out token response,
what I will do is I'll actually call the JSON method here.
So let's grab this, R what, run it again.
Now you'll actually see here in the response, we have our token key,
and then the corresponding value.
In fact, what I also have here is this warning.
So let me just disable this.
So all I've done is paste in this line here.
That's what we made available to you within the scale itself.
And it's certainly not mandatory.
It's just to clean up the output a little bit, OK?
So now if I run the script again, all we're getting is the token key WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
So now if I run the script again, all we're getting is the token key
and then the corresponding value.
So what I can do, therefore, is I can actually call the token key.
And as opposed to printing this off, I can just pass it into a variable, OK?
So if I now print out the token, save here, add it up.
Now I'm just getting the token value and it is available within this little
variable value.
All right, so now we have this information.
Let's go and continue on our script.
So let's go and define the endpoint that I want to be targeting.
So once again, let's grab the URL for the always on the sandbox. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
So once again, let's grab the URL for the always on the sandbox.
Paste you in.
And then I'll save forward slash and just append the actual endpoint that I
want to get.
So in my case here, let's maybe go with what we saw before in the previous
video.
So there's say forward slash DNA, forward slash intense, forward slash API,
forward slash
V1, and then let's go with network hyphen device.
Again, you can get the full list of these endpoints and the documentation.
Right.
So crucially, once we have this token, do you remember what we have to do with WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
So crucially, once we have this token, do you remember what we have to do with
it?
Yep, that's correct.
We're going to have to define that token within our headers.
Now the actual header is again, really remember this is going to be our X off
token header, OK?
So we want to define this within all of our future requests.
Let's go and do that then, OK?
So I will say headers are equal to, and I'll have my curly braces for my
dictionary object.
And the key is going to be X hyphen off hyphen token and the corresponding WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
And the key is going to be X hyphen off hyphen token and the corresponding
value is simply
going to be the token that I've parsed out above right here, OK, which is what
I got
on this line right here, OK?
So if it's now done, what I should be able to do is to make the request to the
target
device.
Let's just call this endpoint response, OK?
And all I will do is once again take the request library, this time I'm going
to send
a GET request, and I'll just pass in the values that I saw before, or say the
URL is
equal to the endpoint URL this time.
The headers are equal to the headers that I have to find above.
And I will say verify is equal to false because we don't care about the self- WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
And I will say verify is equal to false because we don't care about the self-
signed certificates.
So if this is now done, if I just print out the endpoint response, save here,
and now
if I run script one, hit enter.
Once again, we get a 200 response and what I can do therefore is print out the
JSON object
of that response.
So let's grab the JSON method, try it one more time and hit enter.
And look at this right here, but actually getting back all of the information
in programmatic
form.
And in fact, as you know, what I like to do to make things a little bit more WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
And in fact, as you know, what I like to do to make things a little bit more
readable
is say from rich import print as our print.
Again, this is not mandatory.
It's just going to make the output a little bit prettier.
It's also PIT 3 install rich.
And then go here and I will just simply our print the response.
So let's save this, clear the screen, run it again.
And now we get the information back, colorized with the correct indentation,
much easier to
look at.
So if we scroll up to the very top, what we can see is we have this outer key
called
response.
Let's grab this and parse it away, save here. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Let's grab this and parse it away, save here.
And again, that pulls away the outer key.
And now we have a list object of every individual item.
So if we look to the list, we're going to be able to access these individual
details,
such as the MAC address key, which will return the MAC address for that
particular device
or the hostname key to return the hostname or perhaps maybe even the management
IP address.
So let's save this output as a variable here.
The result is equal to, I can get rid of these parentheses now.
And all I will do is look through each object within this result, so I'll say WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
And all I will do is look through each object within this result, so I'll say
for device
and results.
What I can do is print out the device I'm looping through and then just grab
the keys
that I want, such as the hostname.
And then again, let's get that MAC address key.
And then also, let's get the management IP address.
So let's save this.
I'll go up, try again.
And as we can see, we can now programmatically access the information about the
devices within
DNA sensor.
So we can see switch one has a MAC address of this value and the corresponding
management WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
management
IP of this, whereas switch two has this value, switch three and switch four
have these values.
So as you can see, whether we happen to be utilizing Postman or in this case
here Python,
the process is really quite simple.
We're going to have to get that token.
So we need to specify our username and password right here.
We're going to pass that in as basic authentication targeting this very same
endpoint right here,
the token endpoint.
And we send that with a post request.
Remember, not a get request.
We're going to be able to retrieve that token. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
We're going to be able to retrieve that token.
Once we have that token, we can just use it to target any other endpoint we saw
wish
and we pass it in to that ex-auth token header.
As we can see right here and from that point on, we can get the information and
then just
manipulate the data programmatically in any way we saw wish.