WEBVTT

1
00:00.000 --> 00:04.160
Now in the previous video, we saw how we could interact with the Cisco DNA

2
00:04.160 --> 00:05.400
Center or Catalyst

3
00:05.400 --> 00:09.400
Center always on Sandbox utilizing Postman.

4
00:09.400 --> 00:13.830
What we're going to do now is translate the process that we did this time to

5
00:13.830 --> 00:14.520
interact

6
00:14.520 --> 00:16.640
with DNA Center with Python.

7
00:16.640 --> 00:19.200
So with that said, let's just get to it.

8
00:19.200 --> 00:20.840
So let's just create a little directory.

9
00:20.840 --> 00:25.800
Let's just call this maybe say, D-N-A-C-D, enter this directory.

10
00:25.800 --> 00:29.720
All I will do is I'll create a little script called script1.py.

11
00:29.720 --> 00:31.080
Let's go and open it up.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

11
00:29.720 --> 00:31.080
Let's go and open it up.

12
00:31.080 --> 00:36.150
All right, so PopQuiz, which library are we going to use in order to send HTTP

13
00:36.150 --> 00:36.840
requests

14
00:36.840 --> 00:40.320
with Python if you said the request library, well you're absolutely correct.

15
00:40.320 --> 00:41.560
Let's go and do so.

16
00:41.560 --> 00:44.720
Now a few things to actually specify.

17
00:44.720 --> 00:49.440
The very first thing I will do is denote what my username and password is.

18
00:49.440 --> 00:52.280
Remember, the first thing we're going to have to do is to use basic

19
00:52.280 --> 00:53.240
authentication

20
00:53.240 --> 00:56.800
with a post request to get that token.

21
00:56.800 --> 00:57.760
So let's do that first thing.

22
00:57.760 --> 00:59.360
Let me define my username.

23
00:59.360 --> 01:02.320
And I'll just say the username is going to be DevNet user.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

23
00:59.360 --> 01:02.320
And I'll just say the username is going to be DevNet user.

24
01:02.320 --> 01:06.800
And the password is going to be Cisco 123 with an exclamation mark, OK?

25
01:06.800 --> 01:09.640
Now let's define what the token URL is going to be.

26
01:09.640 --> 01:13.050
So in my case here, I'm going to get the information of the Cisco always on

27
01:13.050 --> 01:13.880
sandbox.

28
01:13.880 --> 01:18.200
So let's go back to this page here, grab here, and I'll just paste it in.

29
01:18.200 --> 01:19.480
And I will say forward slash.

30
01:19.480 --> 01:22.760
And if you recall, the actual endpoint for the authentication,

31
01:22.760 --> 01:26.080
as per the documentation, let's go to the quick start.

32
01:26.080 --> 01:30.280
And the authentication, we can see it is this part here.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

32
01:26.080 --> 01:30.280
And the authentication, we can see it is this part here.

33
01:30.280 --> 01:31.280
So grab this.

34
01:31.280 --> 01:32.240
All right, paste it in.

35
01:32.240 --> 01:35.200
And let's get our game rid of this double forward slash.

36
01:35.200 --> 01:36.360
There we go.

37
01:36.360 --> 01:37.840
OK, so check this out.

38
01:37.840 --> 01:40.720
What I'm going to do is to send my request.

39
01:40.720 --> 01:43.880
So to say token response is equal to.

40
01:43.880 --> 01:46.680
And all I will do is take my request library.

41
01:46.680 --> 01:49.120
I'm going to send a post request, remember.

42
01:49.120 --> 01:51.960
And I'm going to pass in some particular parameters here.

43
01:51.960 --> 01:56.480
So also the URL is equal to the token URL I have defined above.

44
01:56.480 --> 01:57.600
I'll specify the auth.

45
01:57.600 --> 02:01.640
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

45
01:57.600 --> 02:01.640
And I'm going to put this within parentheses because it's going to be a tuple.

46
02:01.640 --> 02:05.280
And I'll specify I'm going to put in my username and password I have defined

47
02:05.280 --> 02:06.000
above.

48
02:06.000 --> 02:09.310
And I'll also specify that verification is going to be false because I don't

49
02:09.310 --> 02:09.560
care

50
02:09.560 --> 02:11.720
about these self-sounds certificates.

51
02:11.720 --> 02:16.680
So right now, if I just happened to print out the token response and save this,

52
02:16.680 --> 02:18.400
and let's go and run the script.

53
02:18.400 --> 02:20.280
So I'll say Python 3 script 1.

54
02:20.280 --> 02:23.400
And as we can see here, I do actually get a 200 response.

55
02:23.400 --> 02:25.800
This indicates that we have a successful request,

56
02:25.800 --> 02:28.920
but we actually want to get the information coming back.

57
02:28.920 --> 02:30.400
So let's go and check this out.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

57
02:28.920 --> 02:30.400
So let's go and check this out.

58
02:30.400 --> 02:34.240
Then what I will do here is as opposed to printing out token response,

59
02:34.240 --> 02:37.520
what I will do is I'll actually call the JSON method here.

60
02:37.520 --> 02:40.080
So let's grab this, R what, run it again.

61
02:40.080 --> 02:43.000
Now you'll actually see here in the response, we have our token key,

62
02:43.000 --> 02:44.320
and then the corresponding value.

63
02:44.320 --> 02:46.520
In fact, what I also have here is this warning.

64
02:46.520 --> 02:48.000
So let me just disable this.

65
02:48.000 --> 02:50.600
So all I've done is paste in this line here.

66
02:50.600 --> 02:53.280
That's what we made available to you within the scale itself.

67
02:53.280 --> 02:54.920
And it's certainly not mandatory.

68
02:54.920 --> 02:57.320
It's just to clean up the output a little bit, OK?

69
02:57.320 --> 03:01.080
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

69
02:57.320 --> 03:01.080
So now if I run the script again, all we're getting is the token key

70
03:01.080 --> 03:02.520
and then the corresponding value.

71
03:02.520 --> 03:07.600
So what I can do, therefore, is I can actually call the token key.

72
03:07.600 --> 03:12.480
And as opposed to printing this off, I can just pass it into a variable, OK?

73
03:12.480 --> 03:16.840
So if I now print out the token, save here, add it up.

74
03:16.840 --> 03:21.240
Now I'm just getting the token value and it is available within this little

75
03:21.240 --> 03:22.440
variable value.

76
03:22.440 --> 03:24.200
All right, so now we have this information.

77
03:24.200 --> 03:26.680
Let's go and continue on our script.

78
03:26.680 --> 03:29.760
So let's go and define the endpoint that I want to be targeting.

79
03:29.760 --> 03:33.960
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

79
03:29.760 --> 03:33.960
So once again, let's grab the URL for the always on the sandbox.

80
03:33.960 --> 03:35.200
Paste you in.

81
03:35.200 --> 03:38.840
And then I'll save forward slash and just append the actual endpoint that I

82
03:38.840 --> 03:39.400
want to get.

83
03:39.400 --> 03:43.370
So in my case here, let's maybe go with what we saw before in the previous

84
03:43.370 --> 03:44.000
video.

85
03:44.000 --> 03:48.580
So there's say forward slash DNA, forward slash intense, forward slash API,

86
03:48.580 --> 03:49.200
forward slash

87
03:49.200 --> 03:52.760
V1, and then let's go with network hyphen device.

88
03:52.760 --> 03:56.480
Again, you can get the full list of these endpoints and the documentation.

89
03:56.480 --> 03:57.000
Right.

90
03:57.000 --> 04:01.520
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

90
03:57.000 --> 04:01.520
So crucially, once we have this token, do you remember what we have to do with

91
04:01.520 --> 04:01.880
it?

92
04:01.880 --> 04:02.760
Yep, that's correct.

93
04:02.760 --> 04:06.040
We're going to have to define that token within our headers.

94
04:06.040 --> 04:11.790
Now the actual header is again, really remember this is going to be our X off

95
04:11.790 --> 04:13.080
token header, OK?

96
04:13.080 --> 04:16.560
So we want to define this within all of our future requests.

97
04:16.560 --> 04:18.400
Let's go and do that then, OK?

98
04:18.400 --> 04:23.580
So I will say headers are equal to, and I'll have my curly braces for my

99
04:23.580 --> 04:25.120
dictionary object.

100
04:25.120 --> 04:30.880
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

100
04:25.120 --> 04:30.880
And the key is going to be X hyphen off hyphen token and the corresponding

101
04:30.880 --> 04:32.240
value is simply

102
04:32.240 --> 04:36.240
going to be the token that I've parsed out above right here, OK, which is what

103
04:36.240 --> 04:36.480
I got

104
04:36.480 --> 04:38.920
on this line right here, OK?

105
04:38.920 --> 04:42.910
So if it's now done, what I should be able to do is to make the request to the

106
04:42.910 --> 04:43.400
target

107
04:43.400 --> 04:44.520
device.

108
04:44.520 --> 04:47.360
Let's just call this endpoint response, OK?

109
04:47.360 --> 04:50.760
And all I will do is once again take the request library, this time I'm going

110
04:50.760 --> 04:51.160
to send

111
04:51.160 --> 04:54.820
a GET request, and I'll just pass in the values that I saw before, or say the

112
04:54.820 --> 04:55.160
URL is

113
04:55.160 --> 04:57.440
equal to the endpoint URL this time.

114
04:57.440 --> 04:59.840
The headers are equal to the headers that I have to find above.

115
04:59.840 --> 05:03.410
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

115
04:59.840 --> 05:03.410
And I will say verify is equal to false because we don't care about the self-

116
05:03.410 --> 05:04.920
signed certificates.

117
05:04.920 --> 05:09.790
So if this is now done, if I just print out the endpoint response, save here,

118
05:09.790 --> 05:10.320
and now

119
05:10.320 --> 05:12.720
if I run script one, hit enter.

120
05:12.720 --> 05:17.620
Once again, we get a 200 response and what I can do therefore is print out the

121
05:17.620 --> 05:18.400
JSON object

122
05:18.400 --> 05:19.400
of that response.

123
05:19.400 --> 05:23.840
So let's grab the JSON method, try it one more time and hit enter.

124
05:23.840 --> 05:27.120
And look at this right here, but actually getting back all of the information

125
05:27.120 --> 05:27.920
in programmatic

126
05:27.920 --> 05:28.920
form.

127
05:28.920 --> 05:31.750
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

127
05:28.920 --> 05:31.750
And in fact, as you know, what I like to do to make things a little bit more

128
05:31.750 --> 05:32.200
readable

129
05:32.200 --> 05:35.640
is say from rich import print as our print.

130
05:35.640 --> 05:36.960
Again, this is not mandatory.

131
05:36.960 --> 05:39.320
It's just going to make the output a little bit prettier.

132
05:39.320 --> 05:41.960
It's also PIT 3 install rich.

133
05:41.960 --> 05:45.160
And then go here and I will just simply our print the response.

134
05:45.160 --> 05:48.960
So let's save this, clear the screen, run it again.

135
05:48.960 --> 05:53.090
And now we get the information back, colorized with the correct indentation,

136
05:53.090 --> 05:53.800
much easier to

137
05:53.800 --> 05:54.800
look at.

138
05:54.800 --> 05:57.890
So if we scroll up to the very top, what we can see is we have this outer key

139
05:57.890 --> 05:58.280
called

140
05:58.280 --> 05:59.280
response.

141
05:59.280 --> 06:01.960
Let's grab this and parse it away, save here.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

141
05:59.280 --> 06:01.960
Let's grab this and parse it away, save here.

142
06:01.960 --> 06:04.480
And again, that pulls away the outer key.

143
06:04.480 --> 06:08.360
And now we have a list object of every individual item.

144
06:08.360 --> 06:11.470
So if we look to the list, we're going to be able to access these individual

145
06:11.470 --> 06:12.080
details,

146
06:12.080 --> 06:15.210
such as the MAC address key, which will return the MAC address for that

147
06:15.210 --> 06:16.480
particular device

148
06:16.480 --> 06:21.580
or the hostname key to return the hostname or perhaps maybe even the management

149
06:21.580 --> 06:22.600
IP address.

150
06:22.600 --> 06:25.320
So let's save this output as a variable here.

151
06:25.320 --> 06:29.440
The result is equal to, I can get rid of these parentheses now.

152
06:29.440 --> 06:33.640
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

152
06:29.440 --> 06:33.640
And all I will do is look through each object within this result, so I'll say

153
06:33.640 --> 06:34.160
for device

154
06:34.160 --> 06:35.720
and results.

155
06:35.720 --> 06:38.250
What I can do is print out the device I'm looping through and then just grab

156
06:38.250 --> 06:38.640
the keys

157
06:38.640 --> 06:40.320
that I want, such as the hostname.

158
06:40.320 --> 06:43.960
And then again, let's get that MAC address key.

159
06:43.960 --> 06:47.360
And then also, let's get the management IP address.

160
06:47.360 --> 06:48.360
So let's save this.

161
06:48.360 --> 06:50.080
I'll go up, try again.

162
06:50.080 --> 06:53.680
And as we can see, we can now programmatically access the information about the

163
06:53.680 --> 06:54.560
devices within

164
06:54.560 --> 06:55.880
DNA sensor.

165
06:55.880 --> 06:59.660
So we can see switch one has a MAC address of this value and the corresponding

166
06:59.660 --> 07:00.280
management
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

166
06:59.660 --> 07:00.280
management

167
07:00.280 --> 07:04.540
IP of this, whereas switch two has this value, switch three and switch four

168
07:04.540 --> 07:06.200
have these values.

169
07:06.200 --> 07:09.710
So as you can see, whether we happen to be utilizing Postman or in this case

170
07:09.710 --> 07:10.480
here Python,

171
07:10.480 --> 07:12.200
the process is really quite simple.

172
07:12.200 --> 07:14.200
We're going to have to get that token.

173
07:14.200 --> 07:17.200
So we need to specify our username and password right here.

174
07:17.200 --> 07:22.340
We're going to pass that in as basic authentication targeting this very same

175
07:22.340 --> 07:23.480
endpoint right here,

176
07:23.480 --> 07:25.000
the token endpoint.

177
07:25.000 --> 07:26.880
And we send that with a post request.

178
07:26.880 --> 07:28.080
Remember, not a get request.

179
07:28.080 --> 07:30.400
We're going to be able to retrieve that token.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

179
07:28.080 --> 07:30.400
We're going to be able to retrieve that token.

180
07:30.400 --> 07:34.680
Once we have that token, we can just use it to target any other endpoint we saw

181
07:34.680 --> 07:35.040
wish

182
07:35.040 --> 07:38.720
and we pass it in to that ex-auth token header.

183
07:38.720 --> 07:41.930
As we can see right here and from that point on, we can get the information and

184
07:41.930 --> 07:42.440
then just

185
07:42.440 --> 07:46.320
manipulate the data programmatically in any way we saw wish.
