In this video,
you'll create a script that displays host metadata from AWS and Lennox.
Our plan is to create a bash script that will print to the screen important host
information.
You will learn how to use the EC two instance metadata service and bash
fundamentals to produce the output. Finally,
you'll test the script and copy it to a shared location in the file system.
Let's get started. Okay,
so here we are back in our terminal. Um,
we're gonna create a system information script.
This is something that maybe as a system administrator you could,
you would use to display information or maybe you display the output of this,
um, every time you log in via s ssh. To do this,
we're gonna use some commands from um, bash and or hostos.
And also we're gonna use, um,
a thing called IMDs or instance metadata service from aws.
So the IMDs is an instance meditative service that contains
specific information about your e c two instance.
It can be used to query info about your host that can be used in application or
scripts like this. This information is also available in the AWS web console,
but it is provided to you as a service running accessible inside your
e c two instance. And of course, we're gonna use to cherry pick some data to,
uh, pull some things out.
So the instance metadata service is running on a specific IP address that you
have access to. Um, you saw this route,
this route added when we were looking in IP routes back in the network video and
you it was the 1 69 address.
And what that is doing is it provides this service that you can use, um,
any kind of HTTP client and in our case, we're gonna use Curl comes with Lennox.
It's easy to use.
And so what I'll do is I'm gonna go ahead and paste the URL so you can kind of
take a look at it. Okay,
so here we're using the curl command.
We're using a dash s here just to make the output only quiet.
We'll even want what's coming from the metadata service.
You can see that we're using H E T P colon and this is denoting to curl that.
Hey, this is a website, a web server that we're hitting.
You can see the addresses that 1 69.
This matches the route table that we had that we saw on the network video
and we could see that we're hitting a path of latest and then metadata.
And so if I hit return,
this will give us all the top level options that we have out of this metadata
service. And so you can see here we have, oh, as you can see here,
we have a lot of cut as you can see here.
We have a lot of data that is unique to this instance. And um,
you can see here starting out with the AMI id, um,
that is the OS image we use to build our image that we're on.
Now when we, um, in Terraform,
you have other things like hostname,
your local i p V four address. There's metrics,
there's profile.
So anything with a slash on it means that there's a whole other directory,
a whole other options, a path that you could do.
And you could just add those in like we did here and you can search those.
So we're not gonna go into this too heavily.
We're gonna cherry pick two things from here that we're gonna use in our system
output script,
but I wanted you to see that you have all this at your fingertips whenever you
wanted to look at it. So, okay, I'll lemme clear the screen.
Alright, so to get started,
we are going to go back into our get UB repo that
we cloned, um, that comes with a course and we're gonna go to section,
I can spell correctly section five,
and we're gonna go to system underscore info.
Sh this is the script that we're gonna execute and is gonna print all our
information. So I'm gonna go ahead and open it in the Vim editor. So
let me go up to the top here.
So as you can see basically what's in the output of the screen,
there's obviously a lot going on here and I don't want you to get caught hung,
too,
hung up on a lot of the commands being run in here and some of the host info
stuff that we're doing.
It's okay to not have a good grasp on all these commands as of yet.
But just to know that these are just a way that you could fill some of these,
um, get cut that this is a way that you could get some of this information and
assign it to these variables. Okay,
so let's just go ahead and start here. And we're gonna start here on line four.
And as you can see we have a variable called user.
And you notice that we're using the dollar sign open pers and if you recall,
basically that when you have a doll sign pers in a bash script,
anything that's inside the PERS will be expanded or executed.
And so for that case it's just gonna run the who am I command,
which basically just tells the, uh,
it'll print out who the current user is, who your logged in is.
We also have an active user count and basically we're using the user's command
to grab, um, how many users are logged in. And then we just pipe that to the,
uh, word count, uh, command line.
And then we grab just how many active users there are. Here we are using uh,
the, you name command line six to grab us, uh, the host name
here, line seven.
We're using the DF command and don't worry about all the pipes going on here and
all these other commands.
Basically alls we're doing is we're grabbing all the disks locally.
We're grabbing the one that's the main disk, which in this case is xbd A one.
And then we're just ripping out how much is left,
like how much we've used on the disk
here. Line eight,
we have the release and this is just a file that's on the system and Amazon
Lennox provides us file and it just basically gives us what the release is.
So we go down line 11 and 12, and this is where, where we saw,
when we're looking at the instance metadata service here,
we're we're hitting a couple things.
So in this variable we're grabbing the instance id.
And if you remember from the AWS console when we first created the lab,
every E c two instance has a unique id.
And if you ever wanna do anything about this,
ID like grab some metrics or apply some other,
you're gonna need the unique ID to do, uh,
some work when you do using the AWS command line.
So this will go ahead and grab it for us, uh,
right there and populate that variable. Here's the AMI id,
this is the Omni the OS image we're using that built up this lab
post. And as you see here, we're just populating the A I I D
lines 15 through 18 are just grabbing system stuff here we're grabbing the
memory available and then line 16,
grabbing the total and we're just showing how much is available out of the
total. This is not an exact stuff because of buffers and cash,
but you get the idea. Just a, a quick glance overview,
17 grabs the swap usage using the free command. And then 18,
which is all processes, is just using the PS command,
which basically prints out all the process tree and then it's just trying to
grab him. And then here we're we're piping to the familiar word count to get a,
to get a total number. So this would be like 50 or a hundred or whatever.
So lines 21 through 30 are basically taking the date command,
grabbing the hour and then deciding whether if is it evening,
morning or afternoon.
The only thing that's interesting here is something we didn't cover.
And then when we looked at the if else's in the first video is the, well,
when we're doing branching here you have an eif, which means else if,
so if the first one line 22 doesn't happen,
let's do one more test here on line 25 basically saying if the hour is less
than 17 but greater than or equal to 12,
then it's afternoon and then you have line 28 through 30 that basically says if
it doesn't match 22 or 25, then it's gotta be evening.
So there we go. Let's move on to line 32.
Um, so 33 through 37. Um, this is us describing, if you remember,
uptime and load during when we looked at top.
This is basically just grabbing the uptime, how long this host has been up.
And if you notice here we ha we're using the double parentheses again and that
is to do the arithmetic expression.
And so we do some math there and this is all just basic math to try to get
hours, minutes, days, and seconds out of the uptime.
So lines 40 through 42 are just doing the load average like we saw on top the
one minute, five minute and 15 minute average. Okay, so here we go.
I'm gonna go all the way to the bottom here.
And so this is US lines 44 through 50 is us printing out a banner.
Now this could be any banner, I just picked some asky art that says AWS lab,
but you can use anything you want or you can get rid of it completely
and you can see line 50. We do the good time, which is good evening,
good morning, good afternoon.
And then to the user here,
lines 52 through 68 is this is where the meat of the, the app is.
This is where we're actually gonna print out.
And then this is the screen that we wanna show basically 44 through 68 will what
what will be shown in the standard out and uh,
I won't go through each of them,
but you can see here we we're doing the familiar doll sign curly brace,
which is the expansion of the variable. So whatever the host's name variable is,
it'll expand it there and then it'll just be that will, it will be,
I use just a simple format of the equal signs to try to build some sort of
structure around the output so it wasn't just a bunch of texts dumped out.
And then here I try to line it all up so that it looked y'all uniformed.
Here we go down, you have how many active users the release,
the instance Id obviously lines 55 and 56 are from the instance metadata
service, the current user swap and use the processes uptime.
We go through the days,
hours and minutes and seconds and then there it tells us all the disk remaining.
Okay, so I'm gonna close this file
and I'm gonna clear the screen.
And so what we're gonna do is we're gonna run this now
and this will go ahead and should print out the system information to us.
Okay? And here we are and you can see that it printed out our ask e banner.
Good evening EC two user cuz it figures. That's where I'm at.
Here's the host name,
here's the instance ID and the AMI id and this is what it grabbed from the
instance metadata service. Um, if you went into uh, Terraform,
you could see this or if you went to AWS web console,
these two would also match for our running e c two instance. You have a release,
how many current users are logged in the current user right now,
which is EC two user and then the load average.
You could see here not much really going on. This is, uh, you know,
just for this, the course, we're not doing a lot of heavy lifting on this host.
Here are the memory used.
And so we have this many kilobytes 603,384 out of a total
987,916 kilobytes. We have no swapping use.
So there's 104 processes actually running right now.
And this host has been up for seven days and there is 6.4 gigabytes of
dis space remaining on our root file system.
Okay? So while this is great that it's in our home directory, you know,
we want to share this script with other people. There's nothing very, uh,
sensitive about it in nature. So as,
as you remember when we talked about the file system hierarchy,
a good place to put this is gonna be under user, local,
and even a better place for an executable would be user local bin. So I'm gonna
hit the upper arrow or bring the command back up and I'm,
there's another shell trick you can do here. You can do control a,
which will bring you to the front of the line you're in.
And then what we're gonna do is we're gonna use pseudo to copy this
and we have to use pseudo because it, it is a protected directory.
Then we're gonna use the control e-com command to bring us to the end of the
line and then we're gonna tell it where to put it. U sr, user,
local and then Ben. Okay, so we're gonna copy this file over there.
So let's double check that it is there. Oops, excuse me.
And it should be there and it should be,
what we want to see here is that it is executable by anybody so anybody can run.
It doesn't have to be.
So I'm gonna clear the screen and let's see if system info is
in our path.
You see, I didn't have to prefix it with the uh,
companion repository anymore because now it's in our path.
I can just type it out and bash will search all those known paths and it
searched user, local bin to see if was there and it was and it ran it.
And as you can see here, we do have a difference in the last output.
You can see here on the load we have a zero point 10,
0.03 0.01 for the one minute five,
15 minute respectively on the average.
And we still have one user logged in,
which is really good cuz I'm the only one on this host and it would be a little
shocking if there was more. Okay,
so you've created your first system administration script and you could use
this, like I said, to display when you log in for a message of the day,
you could, uh, have this run on Aron job.
You could have it put out to a log file or you could just run it every now and
then just to see how things are going.
Instead of having to go to multiple places to get this information,
you've created one script to bring it all together. Okay,
let's jump back over and let's do a, a recap of what we've done. Okay,
so let's go ahead and talk about what we did in this video.
So you learned about querying the instance metadata service and how you could
find unique information about your e c two instance.
You explored the system info file to see how it fetched all the metadata and
displayed it all to one screen. Finally,
you tested the script and you copied it to use a local bin for later shared use.
Well, congratulations. You have finished section five.
In this section you learned about the bash shell and how to use common paradigms
like data structures, flow control, and functions.
You applied this knowledge to administer your e c two instance with Bash.
In the next section, we will talk about host level security. See you soon.