WEBVTT

1
00:00.000 --> 00:03.280
Alright friends, let's talk about more of the actual front stuff then.

2
00:03.280 --> 00:06.800
So if you happen to recall the earlier skill we talked about Ansible,

3
00:06.800 --> 00:10.640
in this particular scenario we're doing kind of the same thing just using a

4
00:10.640 --> 00:11.760
different utility.

5
00:11.760 --> 00:16.240
What I've got here is an Ubuntu Linux machine and out here are just some basic

6
00:16.240 --> 00:18.000
Cisco iOS switches.

7
00:18.000 --> 00:25.360
Now all of this is on my local network of 192.168.1.0/24 and each of these

8
00:25.360 --> 00:26.560
devices here have their

9
00:26.560 --> 00:32.320
own management IP address I believe. This one here is .101. This is .102.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

9
00:26.560 --> 00:32.320
own management IP address I believe. This one here is .101. This is .102.

10
00:32.320 --> 00:38.580
This one is 103 and this one is 104. Now all of the devices should have a login

11
00:38.580 --> 00:39.280
user name of

12
00:39.280 --> 00:44.130
admin as well as a password of admin pass and they're going to be reached over

13
00:44.130 --> 00:44.960
port 22

14
00:44.960 --> 00:48.640
utilizing SSH. So these are the main connection details that we have to

15
00:48.640 --> 00:49.360
understand.

16
00:49.360 --> 00:53.920
The question now therefore is how do we actually use Python over here to talk

17
00:53.920 --> 00:55.200
to the devices

18
00:55.200 --> 00:59.120
over there? Well we're going to be looking at a super super popular and well

19
00:59.120 --> 00:59.840
known network

20
00:59.840 --> 01:05.040
automation utility and this is known as NetMico. NetMico essentially allows us
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

20
00:59.840 --> 01:05.040
automation utility and this is known as NetMico. NetMico essentially allows us

21
01:05.040 --> 01:06.800
to send CLI commands

22
01:06.800 --> 01:12.240
to our target devices over an SSH channel. So given the fact that these are old

23
01:12.240 --> 01:12.240
,

24
01:12.240 --> 01:17.610
dusty devices that are not capable of modern automation, utilizing SSH with Net

25
01:17.610 --> 01:18.800
Mico is a very,

26
01:18.800 --> 01:23.740
very good solution. So let's take that information and begin writing our script

27
01:23.740 --> 01:23.920
.

28
01:23.920 --> 01:27.960
Now just in a similar way to what we saw in the previous video, we happened to

29
01:27.960 --> 01:28.640
install something

30
01:28.640 --> 01:32.370
called PyYaml and to a virtual environment and this gave us access to that
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

30
01:28.640 --> 01:32.370
called PyYaml and to a virtual environment and this gave us access to that

31
01:32.370 --> 01:33.360
particular library.

32
01:33.360 --> 01:37.170
Now in a similar vein we want to get access to NetMico so all I will say is pip

33
01:37.170 --> 01:38.000
3 install

34
01:38.000 --> 01:42.150
NetMico and this will now install NetMico with the virtual environment once

35
01:42.150 --> 01:42.880
again giving

36
01:42.880 --> 01:46.280
me access to it with thinmypython scripts. So what I will do is just create a

37
01:46.280 --> 01:46.640
new script,

38
01:46.640 --> 01:52.100
let's call this example 4.py and let me show you how we can begin doing this

39
01:52.100 --> 01:52.320
then.

40
01:52.320 --> 01:56.270
So let's open this up, we'll just go in here. So at the very beginning what I

41
01:56.270 --> 01:56.960
will say is

42
01:56.960 --> 02:01.920
from NetMico, what I will do is import the connect handler factory function.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

42
01:56.960 --> 02:01.920
from NetMico, what I will do is import the connect handler factory function.

43
02:01.920 --> 02:06.230
Now in the interest of simplicity all I will do right now is just write the

44
02:06.230 --> 02:07.360
code to connect to

45
02:07.360 --> 02:11.740
one of my devices as opposed dealing with all of them. So right here what I

46
02:11.740 --> 02:13.120
will do is I'll target

47
02:13.120 --> 02:17.210
this one right here, S1 okay. Now before we begin can you remember when we

48
02:17.210 --> 02:18.000
talked about

49
02:18.000 --> 02:22.380
opening files we had this thing called a context handler. Now we happen to use

50
02:22.380 --> 02:23.440
this to open a

51
02:23.440 --> 02:27.650
particular file and then automatically close that file once we were done with

52
02:27.650 --> 02:28.800
it. Now the cool thing

53
02:28.800 --> 02:32.700
is that this doesn't just work with files we can actually use them in the
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

53
02:28.800 --> 02:32.700
is that this doesn't just work with files we can actually use them in the

54
02:32.700 --> 02:34.160
context of managing a

55
02:34.160 --> 02:38.720
connection. So pretty much we can use it to open a connection to a device and

56
02:38.720 --> 02:39.760
then once we are

57
02:39.760 --> 02:44.090
finished gracefully close that connection. So first things first what I'm going

58
02:44.090 --> 02:44.640
to do is to

59
02:44.640 --> 02:48.540
define some particular details that NetMico needs to know. So what I will do

60
02:48.540 --> 02:49.520
here is create a little

61
02:49.520 --> 02:53.930
Python dictionary and the first actual key I'm going to use is going to be the

62
02:53.930 --> 02:54.720
device type.

63
02:54.720 --> 02:58.900
The type of device I'm talking to is going to be a Cisco iOS device so I'll

64
02:58.900 --> 03:00.080
pass in that particular
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

64
02:58.900 --> 03:00.080
pass in that particular

65
03:00.080 --> 03:04.200
value. Then have a comma. The next key is going to be the host key that allows

66
03:04.200 --> 03:05.440
me to specify which

67
03:05.440 --> 03:12.000
IP address I want to target. So I'll say 1,9,2,168,1.101 which is the IP

68
03:12.000 --> 03:14.000
address of the first device.

69
03:14.000 --> 03:18.480
I'll then specify the username key and the username is going to be admin. The

70
03:18.480 --> 03:19.600
next key is going to be

71
03:19.600 --> 03:23.810
the password key and that's going to be admin pass. So let's now utilize my

72
03:23.810 --> 03:25.040
context manager. I will

73
03:25.040 --> 03:28.360
say with connect handler that I'm importing and I'm going to pass in some

74
03:28.360 --> 03:29.520
particular values. I'm

75
03:29.520 --> 03:34.850
going to say the device type is equal to my device object and I'm going to get
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

75
03:29.520 --> 03:34.850
going to say the device type is equal to my device object and I'm going to get

76
03:34.850 --> 03:36.560
the device type key.

77
03:36.560 --> 03:41.480
Then also the host is equal to and I'll take my device dictionary and this time

78
03:41.480 --> 03:42.640
parse out the

79
03:42.640 --> 03:47.570
host key. I'll then say the username is equal to the device and take the

80
03:47.570 --> 03:48.960
username key and then the

81
03:48.960 --> 03:52.630
password is equal to the device and grab the password key. Now what I want to

82
03:52.630 --> 03:54.000
do is I want to store all

83
03:54.000 --> 03:58.360
of this as a simple object and I'll just call this connection okay. So if I hit

84
03:58.360 --> 03:59.920
return here I'm

85
03:59.920 --> 04:04.180
indented for spaces you see that they're 1,2,3,4 so that means anything I do
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

85
03:59.920 --> 04:04.180
indented for spaces you see that they're 1,2,3,4 so that means anything I do

86
04:04.180 --> 04:05.360
from this point is going

87
04:05.360 --> 04:08.960
to be with the open connection. So what I will do here is I will take my

88
04:08.960 --> 04:10.640
connection objects and

89
04:10.640 --> 04:13.970
I'm going to use a particular method in that meco known as send command see

90
04:13.970 --> 04:15.200
this one right here.

91
04:15.200 --> 04:19.460
Pretty much this is the method we use when we want to send a show command to a

92
04:19.460 --> 04:20.000
device and not

93
04:20.000 --> 04:23.670
make a configuration change just simply a show command. So what we'll do is

94
04:23.670 --> 04:24.880
with the parentheses I

95
04:24.880 --> 04:29.170
will specify the show command that I want to run. So let's maybe go with show

96
04:29.170 --> 04:30.000
IP interface
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

97
04:30.000 --> 04:33.710
belief. Now I actually want to save the output of this command so I'll save it

98
04:33.710 --> 04:34.400
to a variable so

99
04:34.400 --> 04:39.450
I'll save results equal to this and all I will now do is print out the result.

100
04:39.450 --> 04:40.160
So let's go and see

101
04:40.160 --> 04:45.290
how this actually works in. So let's run a script Python 3 example 4.py and

102
04:45.290 --> 04:47.120
enter. In BAM as we can see

103
04:47.120 --> 04:50.590
here we're now getting the information back for that particular device we can

104
04:50.590 --> 04:51.360
actually see the

105
04:51.360 --> 04:55.030
IP address right there in the show IP interface brief output. Now in the

106
04:55.030 --> 04:56.480
example right here I happen

107
04:56.480 --> 05:00.240
to write this out any long hand format so you can actually see all of the
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

107
04:56.480 --> 05:00.240
to write this out any long hand format so you can actually see all of the

108
05:00.240 --> 05:01.440
underlying details

109
05:01.440 --> 05:05.270
but see all of this stuff right here we can actually just directly unpack the

110
05:05.270 --> 05:06.240
dictionary just by

111
05:06.240 --> 05:10.600
using the short hand format watch this if I delete all of this and I just say

112
05:10.600 --> 05:11.760
star star and then give

113
05:11.760 --> 05:15.990
the name the dictionary which is device this has the exact same effect of what

114
05:15.990 --> 05:16.800
I just had before

115
05:16.800 --> 05:21.180
which is a lot shorter and a lot smarter to look at. If I just save this and I

116
05:21.180 --> 05:22.000
arrow up and try

117
05:22.000 --> 05:25.830
again BAM we get the exact same information so now we're able to define our

118
05:25.830 --> 05:27.040
information directly

119
05:27.040 --> 05:31.480
within a Python script and we can use the mclu with a context manager to open a
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

119
05:27.040 --> 05:31.480
within a Python script and we can use the mclu with a context manager to open a

120
05:31.480 --> 05:32.480
connection send the

121
05:32.480 --> 05:35.910
command and then gracefully close that connection. What I want to do is a

122
05:35.910 --> 05:36.960
similar thing but this

123
05:36.960 --> 05:41.450
thing utilize multiple different devices so straight away let's try to remove

124
05:41.450 --> 05:42.560
this information

125
05:42.560 --> 05:47.020
from the script itself and move it into its own YAML file so let's go and do

126
05:47.020 --> 05:48.240
that then. Okay such

127
05:48.240 --> 05:52.480
delete you and what I will do is I will create a little file just called

128
05:52.480 --> 05:54.480
inventory dot YAML looking

129
05:54.480 --> 05:58.480
so let's open this up here so let's begin defining our inventory so what I will

130
05:58.480 --> 05:59.280
have here is a

131
05:59.280 --> 06:02.710
key called device at the very top now in the end of the thing I'm going to put
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

131
05:59.280 --> 06:02.710
key called device at the very top now in the end of the thing I'm going to put

132
06:02.710 --> 06:03.520
the same type of

133
06:03.520 --> 06:07.010
information that we just saw before so I'll say the device type is equal to

134
06:07.010 --> 06:08.640
Cisco iOS I'll say the

135
06:08.640 --> 06:16.730
host is equal to 192 168 1.101 I'll have the username as admin and the password

136
06:16.730 --> 06:17.760
as admin password

137
06:17.760 --> 06:22.370
okay so now we have this separate file and what I want to do is I want to read

138
06:22.370 --> 06:23.440
in the content from

139
06:23.440 --> 06:27.570
that particular file so we know how to do this we can import the YAML directory

140
06:27.570 --> 06:28.320
or rather the YAML

141
06:28.320 --> 06:32.540
directory the YAML library and we want to make this information accessible so
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

141
06:28.320 --> 06:32.540
directory the YAML library and we want to make this information accessible so

142
06:32.540 --> 06:33.520
once again we're going

143
06:33.520 --> 06:38.260
to use another context manager so I'll say with open and then I'll specify the

144
06:38.260 --> 06:39.440
name of the file I want

145
06:39.440 --> 06:43.430
to open which in my case here is going to be infantory dot YAML and I'll have

146
06:43.430 --> 06:44.720
my comma we want to open

147
06:44.720 --> 06:49.900
it in read mode and I'll just say as target file which maybe say device info is

148
06:49.900 --> 06:50.800
equal to and I'll

149
06:50.800 --> 06:54.800
take the YAML library use the safe load function and I'll pass in the target

150
06:54.800 --> 06:56.000
file so now I've opened

151
06:56.000 --> 07:00.300
up this particular file and save the content as structured data into this
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

151
06:56.000 --> 07:00.300
up this particular file and save the content as structured data into this

152
07:00.300 --> 07:01.360
variable right here

153
07:01.360 --> 07:05.290
device info so right now if I happen to print device info and for the moment

154
07:05.290 --> 07:06.000
here if I just

155
07:06.000 --> 07:09.680
comment all this out so this portion does not execute okay so this is not going

156
07:09.680 --> 07:10.640
to have any effect

157
07:10.640 --> 07:13.400
anymore and before I do that actually let's make sure I've spelled that

158
07:13.400 --> 07:14.400
correctly that should be

159
07:14.400 --> 07:18.310
dot YAML not dot YAML so now let's try to run the script look at this right

160
07:18.310 --> 07:19.360
here okay see this

161
07:19.360 --> 07:23.700
we have this dictionary object now if I just grab this device key this is going

162
07:23.700 --> 07:24.880
to give me access to

163
07:24.880 --> 07:29.970
all of this inner information okay let's just do this grab this key save you

164
07:29.970 --> 07:31.520
are a up now we have
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

164
07:29.970 --> 07:31.520
are a up now we have

165
07:31.520 --> 07:35.700
access to all the exact same information that we saw before device type the

166
07:35.700 --> 07:37.040
host username as well

167
07:37.040 --> 07:41.630
as a password and we want to now pass this information into netmeco so as

168
07:41.630 --> 07:42.640
opposed to printing this

169
07:42.640 --> 07:45.890
out what I will do is just get rid of this print statements and I'll just say

170
07:45.890 --> 07:46.880
device is equal to

171
07:46.880 --> 07:51.650
whatever it is we're parsing out right there so now if I just grab all this and

172
07:51.650 --> 07:52.800
I uncomment it so

173
07:52.800 --> 07:56.900
it's going to execute once again now what's happening is we open the actual YAM

174
07:56.900 --> 07:58.480
L file we then load it

175
07:58.480 --> 08:03.200
using YAML save it into this particular variable we parse out the top level key
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

175
07:58.480 --> 08:03.200
using YAML save it into this particular variable we parse out the top level key

176
08:03.200 --> 08:04.400
right here device

177
08:04.400 --> 08:07.850
and if you look at the data here this is the top level key meaning that when we

178
08:07.850 --> 08:08.880
parse it out we get

179
08:08.880 --> 08:13.720
access to all of the inner data right here and this is now stored as a variable

180
08:13.720 --> 08:15.120
known as device

181
08:15.120 --> 08:19.840
which we then pass in to netmeco right here which ultimately means you pass all

182
08:19.840 --> 08:21.280
of that data all

183
08:21.280 --> 08:25.110
of this data right here directly into netmeco and netmeco can now connect to

184
08:25.110 --> 08:26.800
this particular device

185
08:26.800 --> 08:30.330
so let's clear the screen arrow up try again and as we can see here we are once
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

185
08:26.800 --> 08:30.330
so let's clear the screen arrow up try again and as we can see here we are once

186
08:30.330 --> 08:31.360
again connecting to

187
08:31.360 --> 08:36.370
our device this time though we're reading it from an inventory YAML file now so

188
08:36.370 --> 08:37.200
far we have made

189
08:37.200 --> 08:40.820
two good steps the first thing we did was connect to a device by directly

190
08:40.820 --> 08:42.000
baking in the connection

191
08:42.000 --> 08:46.070
details within our Python script and then the second one was to load in the

192
08:46.070 --> 08:47.200
information from a

193
08:47.200 --> 08:50.980
separate YAML file now as it transpires right now we are only connecting to a

194
08:50.980 --> 08:52.240
singular device

195
08:52.240 --> 08:56.880
and this is quite frankly not so useful when you have many many devices so how

196
08:56.880 --> 08:57.920
about we modify what

197
08:57.920 --> 09:02.120
is in that inventory YAML file to include the rest of our devices and then we
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

197
08:57.920 --> 09:02.120
is in that inventory YAML file to include the rest of our devices and then we

198
09:02.120 --> 09:02.880
'll talk about how we

199
09:02.880 --> 09:06.540
can introduce a loop into this script to go through all of those devices sequ

200
09:06.540 --> 09:07.760
entially and send that

201
09:07.760 --> 09:10.240
command to all of the devices in the network
