WEBVTT - This file was automatically generated by VIMEO

0
00:00:07.560 --> 00:00:08.200
In this video,

1
00:00:08.200 --> 00:00:11.780
you'll create a script that displays host metadata from AWS and Lennox.

2
00:00:13.500 --> 00:00:16.760
Our plan is to create a bash script that will print to the screen important host

3
00:00:16.760 --> 00:00:17.593
information.

4
00:00:18.000 --> 00:00:21.580
You will learn how to use the EC two instance metadata service and bash

5
00:00:21.780 --> 00:00:24.560
fundamentals to produce the output. Finally,

6
00:00:25.020 --> 00:00:28.280
you'll test the script and copy it to a shared location in the file system.

7
00:00:28.570 --> 00:00:31.460
Let's get started. Okay,

8
00:00:31.460 --> 00:00:34.420
so here we are back in our terminal. Um,

9
00:00:34.710 --> 00:00:36.700
we're gonna create a system information script.

10
00:00:36.700 --> 00:00:39.380
This is something that maybe as a system administrator you could,

11
00:00:39.380 --> 00:00:43.540
you would use to display information or maybe you display the output of this,

12
00:00:43.880 --> 00:00:47.470
um, every time you log in via s ssh. To do this,

13
00:00:47.470 --> 00:00:51.870
we're gonna use some commands from um, bash and or hostos.

14
00:00:52.290 --> 00:00:53.790
And also we're gonna use, um,

15
00:00:53.910 --> 00:00:57.390
a thing called IMDs or instance metadata service from aws.

16
00:00:58.250 --> 00:01:02.950
So the IMDs is an instance meditative service that contains

17
00:01:03.150 --> 00:01:05.270
specific information about your e c two instance.

18
00:01:05.740 --> 00:01:09.840
It can be used to query info about your host that can be used in application or

19
00:01:09.840 --> 00:01:13.960
scripts like this. This information is also available in the AWS web console,

20
00:01:14.300 --> 00:01:19.240
but it is provided to you as a service running accessible inside your

21
00:01:19.280 --> 00:01:23.520
e c two instance. And of course, we're gonna use to cherry pick some data to,

22
00:01:23.700 --> 00:01:24.800
uh, pull some things out.

23
00:01:25.300 --> 00:01:29.840
So the instance metadata service is running on a specific IP address that you

24
00:01:29.840 --> 00:01:32.320
have access to. Um, you saw this route,

25
00:01:32.630 --> 00:01:37.280
this route added when we were looking in IP routes back in the network video and

26
00:01:37.280 --> 00:01:39.320
you it was the 1 69 address.

27
00:01:39.540 --> 00:01:44.360
And what that is doing is it provides this service that you can use, um,

28
00:01:45.100 --> 00:01:49.880
any kind of HTTP client and in our case, we're gonna use Curl comes with Lennox.

29
00:01:49.880 --> 00:01:50.713
It's easy to use.

30
00:01:51.300 --> 00:01:54.720
And so what I'll do is I'm gonna go ahead and paste the URL so you can kind of

31
00:01:54.720 --> 00:01:57.870
take a look at it. Okay,

32
00:01:58.210 --> 00:02:00.150
so here we're using the curl command.

33
00:02:00.160 --> 00:02:03.430
We're using a dash s here just to make the output only quiet.

34
00:02:03.430 --> 00:02:05.670
We'll even want what's coming from the metadata service.

35
00:02:05.970 --> 00:02:10.750
You can see that we're using H E T P colon and this is denoting to curl that.

36
00:02:10.750 --> 00:02:13.270
Hey, this is a website, a web server that we're hitting.

37
00:02:13.570 --> 00:02:15.710
You can see the addresses that 1 69.

38
00:02:15.900 --> 00:02:19.910
This matches the route table that we had that we saw on the network video

39
00:02:20.730 --> 00:02:24.150
and we could see that we're hitting a path of latest and then metadata.

40
00:02:24.570 --> 00:02:25.910
And so if I hit return,

41
00:02:25.940 --> 00:02:29.870
this will give us all the top level options that we have out of this metadata

42
00:02:29.870 --> 00:02:34.710
service. And so you can see here we have, oh, as you can see here,

43
00:02:35.050 --> 00:02:38.860
we have a lot of cut as you can see here.

44
00:02:38.860 --> 00:02:43.340
We have a lot of data that is unique to this instance. And um,

45
00:02:43.760 --> 00:02:47.140
you can see here starting out with the AMI id, um,

46
00:02:47.140 --> 00:02:51.740
that is the OS image we use to build our image that we're on.

47
00:02:51.740 --> 00:02:53.700
Now when we, um, in Terraform,

48
00:02:54.240 --> 00:02:57.230
you have other things like hostname,

49
00:02:57.940 --> 00:03:01.880
your local i p V four address. There's metrics,

50
00:03:02.070 --> 00:03:03.040
there's profile.

51
00:03:03.700 --> 00:03:07.880
So anything with a slash on it means that there's a whole other directory,

52
00:03:08.080 --> 00:03:10.120
a whole other options, a path that you could do.

53
00:03:10.120 --> 00:03:14.560
And you could just add those in like we did here and you can search those.

54
00:03:15.180 --> 00:03:17.560
So we're not gonna go into this too heavily.

55
00:03:17.650 --> 00:03:21.120
We're gonna cherry pick two things from here that we're gonna use in our system

56
00:03:21.120 --> 00:03:21.780
output script,

57
00:03:21.780 --> 00:03:25.280
but I wanted you to see that you have all this at your fingertips whenever you

58
00:03:25.280 --> 00:03:27.880
wanted to look at it. So, okay, I'll lemme clear the screen.

59
00:03:29.970 --> 00:03:32.220
Alright, so to get started,

60
00:03:32.520 --> 00:03:37.460
we are going to go back into our get UB repo that

61
00:03:37.460 --> 00:03:41.620
we cloned, um, that comes with a course and we're gonna go to section,

62
00:03:42.750 --> 00:03:44.750
I can spell correctly section five,

63
00:03:45.330 --> 00:03:48.110
and we're gonna go to system underscore info.

64
00:03:48.570 --> 00:03:53.320
Sh this is the script that we're gonna execute and is gonna print all our

65
00:03:53.320 --> 00:03:57.440
information. So I'm gonna go ahead and open it in the Vim editor. So

66
00:03:59.040 --> 00:04:00.460
let me go up to the top here.

67
00:04:02.280 --> 00:04:06.100
So as you can see basically what's in the output of the screen,

68
00:04:06.100 --> 00:04:09.540
there's obviously a lot going on here and I don't want you to get caught hung,

69
00:04:09.640 --> 00:04:09.860
too,

70
00:04:09.860 --> 00:04:14.180
hung up on a lot of the commands being run in here and some of the host info

71
00:04:14.180 --> 00:04:15.013
stuff that we're doing.

72
00:04:16.090 --> 00:04:19.500
It's okay to not have a good grasp on all these commands as of yet.

73
00:04:19.880 --> 00:04:23.620
But just to know that these are just a way that you could fill some of these,

74
00:04:23.920 --> 00:04:28.460
um, get cut that this is a way that you could get some of this information and

75
00:04:28.520 --> 00:04:32.950
assign it to these variables. Okay,

76
00:04:33.290 --> 00:04:37.270
so let's just go ahead and start here. And we're gonna start here on line four.

77
00:04:37.970 --> 00:04:40.460
And as you can see we have a variable called user.

78
00:04:41.200 --> 00:04:45.500
And you notice that we're using the dollar sign open pers and if you recall,

79
00:04:46.720 --> 00:04:50.690
basically that when you have a doll sign pers in a bash script,

80
00:04:51.530 --> 00:04:54.610
anything that's inside the PERS will be expanded or executed.

81
00:04:55.270 --> 00:04:58.570
And so for that case it's just gonna run the who am I command,

82
00:04:58.570 --> 00:05:01.930
which basically just tells the, uh,

83
00:05:01.980 --> 00:05:05.010
it'll print out who the current user is, who your logged in is.

84
00:05:06.460 --> 00:05:10.360
We also have an active user count and basically we're using the user's command

85
00:05:10.420 --> 00:05:14.560
to grab, um, how many users are logged in. And then we just pipe that to the,

86
00:05:14.660 --> 00:05:16.560
uh, word count, uh, command line.

87
00:05:17.100 --> 00:05:21.830
And then we grab just how many active users there are. Here we are using uh,

88
00:05:21.830 --> 00:05:26.190
the, you name command line six to grab us, uh, the host name

89
00:05:29.010 --> 00:05:30.300
here, line seven.

90
00:05:30.390 --> 00:05:34.980
We're using the DF command and don't worry about all the pipes going on here and

91
00:05:34.980 --> 00:05:35.820
all these other commands.

92
00:05:35.820 --> 00:05:38.820
Basically alls we're doing is we're grabbing all the disks locally.

93
00:05:39.270 --> 00:05:42.740
We're grabbing the one that's the main disk, which in this case is xbd A one.

94
00:05:42.760 --> 00:05:45.020
And then we're just ripping out how much is left,

95
00:05:45.020 --> 00:05:46.860
like how much we've used on the disk

96
00:05:50.100 --> 00:05:50.990
here. Line eight,

97
00:05:50.990 --> 00:05:55.030
we have the release and this is just a file that's on the system and Amazon

98
00:05:55.150 --> 00:05:58.320
Lennox provides us file and it just basically gives us what the release is.

99
00:05:58.740 --> 00:06:02.200
So we go down line 11 and 12, and this is where, where we saw,

100
00:06:02.200 --> 00:06:04.560
when we're looking at the instance metadata service here,

101
00:06:04.560 --> 00:06:06.280
we're we're hitting a couple things.

102
00:06:06.500 --> 00:06:10.280
So in this variable we're grabbing the instance id.

103
00:06:10.280 --> 00:06:13.600
And if you remember from the AWS console when we first created the lab,

104
00:06:14.010 --> 00:06:16.640
every E c two instance has a unique id.

105
00:06:17.060 --> 00:06:20.000
And if you ever wanna do anything about this,

106
00:06:20.220 --> 00:06:22.880
ID like grab some metrics or apply some other,

107
00:06:23.300 --> 00:06:25.680
you're gonna need the unique ID to do, uh,

108
00:06:25.680 --> 00:06:28.360
some work when you do using the AWS command line.

109
00:06:29.100 --> 00:06:31.800
So this will go ahead and grab it for us, uh,

110
00:06:31.800 --> 00:06:35.610
right there and populate that variable. Here's the AMI id,

111
00:06:35.610 --> 00:06:40.130
this is the Omni the OS image we're using that built up this lab

112
00:06:40.520 --> 00:06:44.510
post. And as you see here, we're just populating the A I I D

113
00:06:46.110 --> 00:06:50.300
lines 15 through 18 are just grabbing system stuff here we're grabbing the

114
00:06:50.300 --> 00:06:51.660
memory available and then line 16,

115
00:06:51.660 --> 00:06:55.340
grabbing the total and we're just showing how much is available out of the

116
00:06:55.340 --> 00:06:58.340
total. This is not an exact stuff because of buffers and cash,

117
00:06:58.400 --> 00:07:01.020
but you get the idea. Just a, a quick glance overview,

118
00:07:02.020 --> 00:07:06.660
17 grabs the swap usage using the free command. And then 18,

119
00:07:06.660 --> 00:07:09.180
which is all processes, is just using the PS command,

120
00:07:09.180 --> 00:07:13.460
which basically prints out all the process tree and then it's just trying to

121
00:07:13.460 --> 00:07:16.580
grab him. And then here we're we're piping to the familiar word count to get a,

122
00:07:16.720 --> 00:07:19.780
to get a total number. So this would be like 50 or a hundred or whatever.

123
00:07:20.670 --> 00:07:25.370
So lines 21 through 30 are basically taking the date command,

124
00:07:25.370 --> 00:07:30.040
grabbing the hour and then deciding whether if is it evening,

125
00:07:30.110 --> 00:07:31.320
morning or afternoon.

126
00:07:31.700 --> 00:07:34.240
The only thing that's interesting here is something we didn't cover.

127
00:07:34.460 --> 00:07:38.880
And then when we looked at the if else's in the first video is the, well,

128
00:07:38.880 --> 00:07:42.120
when we're doing branching here you have an eif, which means else if,

129
00:07:42.380 --> 00:07:45.600
so if the first one line 22 doesn't happen,

130
00:07:45.930 --> 00:07:50.840
let's do one more test here on line 25 basically saying if the hour is less

131
00:07:50.840 --> 00:07:54.200
than 17 but greater than or equal to 12,

132
00:07:54.990 --> 00:07:59.640
then it's afternoon and then you have line 28 through 30 that basically says if

133
00:07:59.640 --> 00:08:03.240
it doesn't match 22 or 25, then it's gotta be evening.

134
00:08:03.900 --> 00:08:08.630
So there we go. Let's move on to line 32.

135
00:08:09.370 --> 00:08:13.910
Um, so 33 through 37. Um, this is us describing, if you remember,

136
00:08:14.090 --> 00:08:17.230
uptime and load during when we looked at top.

137
00:08:18.390 --> 00:08:23.080
This is basically just grabbing the uptime, how long this host has been up.

138
00:08:23.260 --> 00:08:26.760
And if you notice here we ha we're using the double parentheses again and that

139
00:08:26.760 --> 00:08:29.440
is to do the arithmetic expression.

140
00:08:31.280 --> 00:08:34.460
And so we do some math there and this is all just basic math to try to get

141
00:08:34.550 --> 00:08:37.020
hours, minutes, days, and seconds out of the uptime.

142
00:08:38.570 --> 00:08:42.590
So lines 40 through 42 are just doing the load average like we saw on top the

143
00:08:42.590 --> 00:08:46.750
one minute, five minute and 15 minute average. Okay, so here we go.

144
00:08:46.770 --> 00:08:47.950
I'm gonna go all the way to the bottom here.

145
00:08:47.950 --> 00:08:52.150
And so this is US lines 44 through 50 is us printing out a banner.

146
00:08:53.040 --> 00:08:57.120
Now this could be any banner, I just picked some asky art that says AWS lab,

147
00:08:57.180 --> 00:08:59.840
but you can use anything you want or you can get rid of it completely

148
00:09:01.870 --> 00:09:05.490
and you can see line 50. We do the good time, which is good evening,

149
00:09:06.000 --> 00:09:08.830
good morning, good afternoon.

150
00:09:09.090 --> 00:09:11.500
And then to the user here,

151
00:09:11.670 --> 00:09:16.460
lines 52 through 68 is this is where the meat of the, the app is.

152
00:09:16.660 --> 00:09:18.060
This is where we're actually gonna print out.

153
00:09:18.080 --> 00:09:22.260
And then this is the screen that we wanna show basically 44 through 68 will what

154
00:09:22.260 --> 00:09:26.780
what will be shown in the standard out and uh,

155
00:09:26.820 --> 00:09:27.980
I won't go through each of them,

156
00:09:28.000 --> 00:09:31.940
but you can see here we we're doing the familiar doll sign curly brace,

157
00:09:31.950 --> 00:09:36.500
which is the expansion of the variable. So whatever the host's name variable is,

158
00:09:36.780 --> 00:09:40.500
it'll expand it there and then it'll just be that will, it will be,

159
00:09:41.280 --> 00:09:45.800
I use just a simple format of the equal signs to try to build some sort of

160
00:09:45.960 --> 00:09:48.760
structure around the output so it wasn't just a bunch of texts dumped out.

161
00:09:49.060 --> 00:09:52.800
And then here I try to line it all up so that it looked y'all uniformed.

162
00:09:54.100 --> 00:09:57.550
Here we go down, you have how many active users the release,

163
00:09:57.570 --> 00:10:02.550
the instance Id obviously lines 55 and 56 are from the instance metadata

164
00:10:02.550 --> 00:10:07.190
service, the current user swap and use the processes uptime.

165
00:10:07.450 --> 00:10:08.310
We go through the days,

166
00:10:08.310 --> 00:10:13.030
hours and minutes and seconds and then there it tells us all the disk remaining.

167
00:10:15.000 --> 00:10:17.050
Okay, so I'm gonna close this file

168
00:10:19.660 --> 00:10:20.840
and I'm gonna clear the screen.

169
00:10:22.150 --> 00:10:24.410
And so what we're gonna do is we're gonna run this now

170
00:10:26.340 --> 00:10:30.520
and this will go ahead and should print out the system information to us.

171
00:10:32.930 --> 00:10:37.020
Okay? And here we are and you can see that it printed out our ask e banner.

172
00:10:37.210 --> 00:10:40.900
Good evening EC two user cuz it figures. That's where I'm at.

173
00:10:41.970 --> 00:10:42.990
Here's the host name,

174
00:10:44.300 --> 00:10:48.680
here's the instance ID and the AMI id and this is what it grabbed from the

175
00:10:48.840 --> 00:10:53.120
instance metadata service. Um, if you went into uh, Terraform,

176
00:10:53.990 --> 00:10:56.250
you could see this or if you went to AWS web console,

177
00:10:56.250 --> 00:11:00.480
these two would also match for our running e c two instance. You have a release,

178
00:11:00.980 --> 00:11:04.400
how many current users are logged in the current user right now,

179
00:11:04.400 --> 00:11:07.580
which is EC two user and then the load average.

180
00:11:07.580 --> 00:11:10.580
You could see here not much really going on. This is, uh, you know,

181
00:11:10.580 --> 00:11:14.460
just for this, the course, we're not doing a lot of heavy lifting on this host.

182
00:11:14.850 --> 00:11:15.940
Here are the memory used.

183
00:11:16.320 --> 00:11:21.140
And so we have this many kilobytes 603,384 out of a total

184
00:11:21.690 --> 00:11:25.780
987,916 kilobytes. We have no swapping use.

185
00:11:26.240 --> 00:11:29.060
So there's 104 processes actually running right now.

186
00:11:29.640 --> 00:11:34.460
And this host has been up for seven days and there is 6.4 gigabytes of

187
00:11:34.480 --> 00:11:37.580
dis space remaining on our root file system.

188
00:11:39.520 --> 00:11:44.330
Okay? So while this is great that it's in our home directory, you know,

189
00:11:44.590 --> 00:11:48.530
we want to share this script with other people. There's nothing very, uh,

190
00:11:48.530 --> 00:11:50.730
sensitive about it in nature. So as,

191
00:11:50.730 --> 00:11:53.050
as you remember when we talked about the file system hierarchy,

192
00:11:53.840 --> 00:11:55.920
a good place to put this is gonna be under user, local,

193
00:11:56.420 --> 00:12:01.080
and even a better place for an executable would be user local bin. So I'm gonna

194
00:12:02.720 --> 00:12:05.180
hit the upper arrow or bring the command back up and I'm,

195
00:12:05.180 --> 00:12:08.060
there's another shell trick you can do here. You can do control a,

196
00:12:08.190 --> 00:12:10.580
which will bring you to the front of the line you're in.

197
00:12:10.960 --> 00:12:14.900
And then what we're gonna do is we're gonna use pseudo to copy this

198
00:12:15.810 --> 00:12:18.430
and we have to use pseudo because it, it is a protected directory.

199
00:12:18.500 --> 00:12:22.230
Then we're gonna use the control e-com command to bring us to the end of the

200
00:12:22.230 --> 00:12:25.990
line and then we're gonna tell it where to put it. U sr, user,

201
00:12:26.680 --> 00:12:31.210
local and then Ben. Okay, so we're gonna copy this file over there.

202
00:12:32.230 --> 00:12:36.650
So let's double check that it is there. Oops, excuse me.

203
00:12:37.850 --> 00:12:41.270
And it should be there and it should be,

204
00:12:41.270 --> 00:12:46.230
what we want to see here is that it is executable by anybody so anybody can run.

205
00:12:46.230 --> 00:12:47.070
It doesn't have to be.

206
00:12:47.570 --> 00:12:52.520
So I'm gonna clear the screen and let's see if system info is

207
00:12:52.520 --> 00:12:53.353
in our path.

208
00:12:59.650 --> 00:13:02.590
You see, I didn't have to prefix it with the uh,

209
00:13:02.590 --> 00:13:05.230
companion repository anymore because now it's in our path.

210
00:13:05.670 --> 00:13:10.430
I can just type it out and bash will search all those known paths and it

211
00:13:10.430 --> 00:13:13.870
searched user, local bin to see if was there and it was and it ran it.

212
00:13:13.970 --> 00:13:18.110
And as you can see here, we do have a difference in the last output.

213
00:13:18.110 --> 00:13:21.230
You can see here on the load we have a zero point 10,

214
00:13:21.900 --> 00:13:26.830
0.03 0.01 for the one minute five,

215
00:13:26.830 --> 00:13:28.830
15 minute respectively on the average.

216
00:13:30.360 --> 00:13:31.940
And we still have one user logged in,

217
00:13:31.940 --> 00:13:34.780
which is really good cuz I'm the only one on this host and it would be a little

218
00:13:34.980 --> 00:13:37.200
shocking if there was more. Okay,

219
00:13:37.460 --> 00:13:42.330
so you've created your first system administration script and you could use

220
00:13:42.330 --> 00:13:46.210
this, like I said, to display when you log in for a message of the day,

221
00:13:46.590 --> 00:13:48.610
you could, uh, have this run on Aron job.

222
00:13:48.830 --> 00:13:52.130
You could have it put out to a log file or you could just run it every now and

223
00:13:52.130 --> 00:13:53.450
then just to see how things are going.

224
00:13:53.640 --> 00:13:58.130
Instead of having to go to multiple places to get this information,

225
00:13:58.130 --> 00:14:00.810
you've created one script to bring it all together. Okay,

226
00:14:00.980 --> 00:14:05.750
let's jump back over and let's do a, a recap of what we've done. Okay,

227
00:14:06.130 --> 00:14:08.590
so let's go ahead and talk about what we did in this video.

228
00:14:09.330 --> 00:14:12.550
So you learned about querying the instance metadata service and how you could

229
00:14:12.550 --> 00:14:15.030
find unique information about your e c two instance.

230
00:14:15.690 --> 00:14:19.870
You explored the system info file to see how it fetched all the metadata and

231
00:14:19.870 --> 00:14:22.820
displayed it all to one screen. Finally,

232
00:14:23.160 --> 00:14:26.940
you tested the script and you copied it to use a local bin for later shared use.

233
00:14:28.530 --> 00:14:31.580
Well, congratulations. You have finished section five.

234
00:14:31.760 --> 00:14:35.580
In this section you learned about the bash shell and how to use common paradigms

235
00:14:35.580 --> 00:14:37.980
like data structures, flow control, and functions.

236
00:14:38.440 --> 00:14:41.940
You applied this knowledge to administer your e c two instance with Bash.

237
00:14:43.040 --> 00:14:47.380
In the next section, we will talk about host level security. See you soon.
