Imran: Hey everyone.
Here is another newer script that we will go over.
It's about checking the other server connectivity.
Of course, you need to know all the time
if you are in a bigger environment,
you need to know if your other servers
are actually reachable and pingable.
Now I know there are a lot of monitoring solutions
that are out there that a lot of companies have.
They actually, those are monitor the servers,
they monitor the health check and everything.
But still once in a while you do need
to actually ping other servers
or remote servers that actually tells you
if they are alive or they are down.
So, this lesson is about pinging other servers.
What are we gonna do is we actually gonna create a script
to check the status of the remote host or hosts
and we'll find out if it is pingable or not.
Very simple script.
We actually have to combine in this script,
we have to combine if-else statements, we might,
we will have to actually combine do-while statement together
and you'll see how we could ping other servers.
I have my Linux machine open
and I also have a quick document here.
You could say a cheat sheet
which already I have a script written there
because if I go over completely blank with you,
I might forget and I might lose time
and I definitely don't wanna do that.
I definitely don't wanna lose my time and your time.
So anyway, the first thing I wanted to do
is I do wanted to mention one thing
that previously we actually created a directory
called "professional-scripts".
Now I noticed this whole directory is a very long directory.
Let's make it short.
So, this way it will be easier to actually change directory
from one place to another.
So, I'm gonna just do
move professional-directory scripts to ps,
as simple as that.
Now, you'll see I have a directory called "ps".
When I go to ps, which stands for professional scripts,
and now I have all my scripts.
So, just to start off with that.
Now let's start creating our first script that is going
to ping an IP address.
Now the IP address that I have selected is the IP address
of my home modem, which is 192.168.1.1.
And most likely if you are in a home environment,
not in the office, then your IP address
is the same as 192.168.1.1.
If it is not the same,
then you could go find out the default gateway IP
just to test if you could ping it or not.
Or you could just ping or you could just pick any IP
that you could test your script against.
So, I have picked one 192.168.1.1.
This is my IP that I'm gonna test.
So, let's first create a script.
And I'm gonna call the script "ping-script".
Okay, and this is the first time I will copy later on.
So, I'm just gonna quickly do the information.
Date.
Description, "This script will ping a remote host
and notify", of course.
Then modified.
And since this is the first time
and I am going to leave that date as.
Okay, now let's get into the real work.
Now did we define the variable?
Now ping.
I'm sure you guys know what a ping-script is.
Ping goes out on the remote host, ping an IP address,
and tell you if a host is available or not.
So, let me just write and quit out of this script.
Make sure you save it and just if you don't know ping
then just type "ping 192.168.1.1"
and you will see you are getting 64 byte back
from this this machine.
It means there is a machine out there with that IP address
and it is reachable.
Anyway, so now let's go back to our script.
So, what we are trying to do with the script
is we want to ping, -c is count, just ping once,
the IP one 192.168.1.1.
Now you, as you go along, you're probably thinking,
"Hey, how come Imran knows
all those different options to specify?
How can he remembers?
How can he just pick really quick?"
Well, to be honest, I don't remember.
There are many times I just go online, I Google it,
and I get the options.
So, if I wanted to Google ping only once,
I'll type, "ping only once in shell script"
or "ping only once a remote host"
and it will give me the option that I'm looking for.
So, don't worry about it.
You're not the one that you're thinking,
"Hey, my memory sucks, I'm not gonna remember that."
Trust me, my memory is probably worse than yours.
So, don't worry about it.
If you forget anything, go online and search for it,
and you will get it as long as you type
exactly what you're looking for.
All right, moving forward.
So, we are pinging this IP, all right, it will ping.
It will ping one time.
Perfect.
Now what will happen?
What are you trying to do when it pings?
Okay, we are trying to say is "if [ $?".
Now what is a "$?"?
$? is the output of all the command that you run.
By default, the $? returns two values.
One is zero, which is that yes, it ran successfully
or anything else.
So, these are the exit status.
If you don't know the differences between exit status,
you could look it up.
But there are mostly two exit status that I usually rely on.
Exit status one and zero.
So, $?, if the return of my command above
which is "ping -c1", the IP address, is equal to zero.
Okay?
You see, if the return value is equal to zero,
which means it did run, it was successful,
then, since we are in if-else-then statements,
we have to specify then, tab.
As I said previously, always put tab
so it will look neat and clean.
Then what do you want it to do?
That, you know, the ping is successful,
the host is reachable.
Then what do you wanna do?
Well simply say, "OK".
How's that?
Just that simple.
All right, perfect.
Now what if you don't get the zero status?
Which is it can't ping it.
Then what do you wanna do?
Then is what else.
If that doesn't work, then what else?
Else "echo NOT OK", right?
So, if do this, else do that.
Okay, perfect.
Now every time you start if, you have to end it with fi
and that's pretty much it.
Save your script,
give executable permissions to your script,
and now run the script.
Let's clear the screen and now run the script.
Perfect.
You see, it ran and it ran one time and it came back.
It was successful.
Statistics came back.
One packet transmitted.
This is all part of ping-script.
So, we came back, the exit status was zero,
which means it ran.
And that's why I said,
and that's why the script said it was okay, perfect.
Now what if I give an IP address,
so I'll change my IP address right here,
I have specified right here again.
I'm gonna change the IP address to 192.168.1.235.
Now how do I get that IP address?
I just pinged some of the IP address in my home network.
I'm sure if you are in a home network,
your IP addresses are probably used
from 2 to like 15 or 20 at least
because we have so many gadgets at home.
But it's very highly unlikely
that you're probably gonna be at 235 IPs.
So, what I did is before I picked that IP,
I just simply pinged that IP.
Okay, so you see I pinged that IP
and that IP is not reachable.
So, that's why I just picked that IP.
You could pick any IP that is not pingable either.
So, I'm gonna go into back into my script
and this time I'm gonna change the IP to
instead of 1.1, I'm gonna change it to 235 and save it.
And now when I run it, what should I get?
It can't ping it, right?
And it's gonna come back and it's gonna say, "NOT OK".
See that's how the if-else script works.
And to ping other hosts, other devices,
that is something that you will definitely,
definitely will be working on I'm sure
if you are in system administrator or engineer
or any kind of role, you probably will have to do that.
Now next one is if you notice right here,
I have this output coming in.
That output, it's gonna give me that it pinged
and then it'll give me the NOT OK.
Now what if I just, I'm just concerned
that don't show me anything, just tell me if it is okay
and if it's not okay.
So, what do we do here?
As I have written down here,
you could output 'cause right here when you do ping,
it output to the screen.
If you do not want that to be output on the screen,
then there is this syntax that you have to specify.
It says "&>" and "dev/null".
It means that you are outputting it to a dev/null.
Dev/null is just a file in Linux.
It's a null file.
Every time you throw anything on dev/null,
it will just disappear.
That's how it is.
And I'm very hopeful you probably heard of that as well.
So, I'm gonna go back into my ping-script
and this time I am going to tell it that,
you know what, don't throw the messages on the screen.
I don't care about the message.
All I care about is what is the outcome.
Is it okay or is it not okay?
Now I will run it.
Now if you see, now let me clear the screen
so you will see it will come up
very nice and neat and clean.
See, it's not throwing all those messages.
All I'm gonna get is "NOT OK".
That's what I care about.
So, many times you don't really wanna see
all that junk on the screen.
Because that will not only confuse you
but it'll take your time to actually read through it
and what exactly you are looking for.
So, if I change the IP back to one,
then I should get "OK", simply and straightforward.
Just "OK".
Perfect.
Now next thing I wanted to cover is what if you wanted
to define that IP in a variable,
meaning you put that IP in front of a variable.
So, instead of just pinging the straight IP,
you ping the variable.
Why do I do that?
Because many times you do want to add multiple host
or, you know, certain things that you want to, you want,
you probably want it to ping multiple times.
Then you don't want to specify IP again and again and again.
You just specify one time
and then let do the host do itself.
So, I'm gonna go back to into ping-script
and now I'm gonna put in variable right here.
And variable I'm gonna do
is hosts="192.168.1.1".
That's my variable.
Now instead of ping the IP,
what I'm gonna say is ping the host and "$hosts".
Right?
And this time I'm gonna make my script a little fancier.
I'm gonna say if ping this host, which is this,
this variable, and I'll put to a dev/null,
if the output is successful, then okay.
Instead of just "OK", I'm gonna say then say,
host, the IP, "is OK".
And else if that IP is not pingable, then host "is NOT OK".
You see, I use my variable so efficiently
now I used it three different, in three different places.
I don't have to specify IP here, sorry, IP here,
I don't have to specify IP here,
I don't have to specify IP here.
I have specified the IP just one time
as a variable right here
and now I'm just gonna use that variable as a,
or that container,
and I'm gonna use it again and again and again.
Okay, so let's run it.
Nice, right?
IP that I'm trying to ping "is OK"
so now it's very specific that it's telling me
that this is the IP that you're trying to ping
and it is okay not to worry about it.
Now what if I change the IP to 235,
which is of course one of my IP that is not pingable,
that is not assigned.
And now let's clear the screen, so we could see better.
Ping-script.
It's gonna ping and it the reason it takes a little longer
because sometimes ping just waits for it to respond.
It's by default, it just waits for it.
Anyways, so you see it came back,
it says, "192.168.1.235 is NOT OK".
Perfect.
See how we actually make the use of variable here
and we use if and else statements
and we are putting the variables in place.
Again, now next one is changing IP.
We already did that.
Perfect.
Now the next one is a little complicated one
but it's easy if you do it together.
Okay?
Now what if you wanted to ping multiple IP address
in one script?
Okay, one IP, I could ping it, right?
I could, I don't even have to create a script
to ping that, right?
I understand.
You're probably thinking that.
But the reason we start off with one IP
is so you would understand
what if you want to ping hundred servers?
Now things get interesting.
Now you really need to have that script, right?
Okay, so first of all what you need to do is you need
to put that IP addresses that you wanted to ping to a file.
So, I'm gonna create a file and I'm gonna call it "myhosts"
and I'm gonna put in the IP address, the 192.168.1.1.
And the second one I'm gonna put it 192.168.1.235.
So, now since we are working on only two IPs,
if you have multiple IPs, you wanna try it,
please go ahead.
Now save.
Now this is just a empty file,
or sorry, this is just a flat file
and let's do myhosts and you'll see it has two IP address.
Now instead of changing the existing script,
I am going to create a new script.
And of course I'm not gonna
create a new script from scratch.
What I'm gonna do is I'm gonna just copy my ping-script
and I'm gonna name it ping-script-all.
So, this way it will identify that this is
for all multiple Linux servers or multiple hosts.
Okay?
Now, "vi ping-script-all".
You see everything here is done
but I just have to change the description.
It says, "This script will ping a remote host".
Not a remote host, "multiple remote hosts".
Make sure you change this description.
It's very important.
Okay?
Now we are gonna come in and what I'm gonna do
is I'm gonna simply, you know what,
let's first get rid of this variable "hosts".
I'm gonna start it from here.
What are those hosts?
So, this is the variable and variable is what you have
to specify the location of your file.
So, that is "/home/iafzal/ps", ps for professional scripts,
remember we did that and we,
what was the name we picked for the flat file?
Was it hosts?
I'm not sure actually.
Let me go check.
So, please make sure this location
and this host file is correct,
otherwise the script is gonna complain.
No, I had specified "myhosts"
and it is "home/iafzal/ps" directory.
"Myhosts".
Okay, perfect.
So, now here is the location of the file
that has all my multiple host.
Okay, perfect.
Now I wanted to use for-loop for that.
Why?
Because for-loop allows you to go through the script once,
twice, or as many times until everything is being read
or everything has been satisfied.
So, if I don't specify a for-loop,
what will happen is it will pick the first IP in the list
and it will be done.
It's not gonna go through the second IP.
That's why we have to specify for-loop, "for IP".
Now why I picked IP, IP is just a container.
Again, it's just a variable.
You could just pick anything.
"For IP in"
and "$" parentheses.
In where?
"Cat" specify "$hosts".
Okay, now you see what I did.
For IP, I'm putting, I'm creating a container,
another container IP in and I'm catting this
at c hosts file.
What is, sorry, not at c host file,
I'm catting this $hosts files.
What is $host file?
$host file is this, "home/iafzal/ps/myhosts"
So, this is what I'm telling, "for IP in",
put everything that comes from this result "cat hosts",
put everything in IP.
All right, perfect, I will do that.
Now what else do you want me to do?
Now do, because you have to specify what exactly,
once you do for always has to have a do.
For-do-done, for-while-do.
Okay, so for, okay, now what do you want me to do?
I'm gonna do is I'm gonna move this one tab over
and I'm gonna say "ping -c1", same thing,
and now instead of hosts, I want it to do IP
because right here it says IP, right?
This is the container, I'm specifying it right here.
Now you're probably thinking,
"Hey Imran, why can't I just use the same variable?"
Hmm, that's a good question.
I don't know, try it.
Maybe it works. (Imran laughs)
But this is how I actually go through my practice
and if you wanted to try it, please be my guest.
Try it.
If it works, perfect.
All right, so IP, okay, going into dev/null
now coming back to our same script.
If #$? equals zero then,
instead of hosts now we are talking about IP.
IP is, I put it there, ping pass, I'll just keep,
leave it as is.
IP "is OK".
And the same thing here, IP is "NOT OK".
So, by the way, this file that I am looking at it,
I will attach that file with this lesson
so you don't have to worry about it.
You could also copy paste if you make a mistake.
Sometimes when you are writing a script,
many times I would say even 80% or 85% of the times,
you make mistakes.
I make mistakes, too.
That's why I try to write it so there won't be any mistakes.
Okay, so this is how it's gonna do again.
So, I hopefully you understand what I'm trying to get at.
This is the variable.
And what variable is gonna do,
it's gonna specify the name of the file.
Okay?
Now for IP, this is another variable
and it's saying that, you know what,
go and read that file, cat that file,
and that file is at c, just $hosts.
I dunno why I keep saying at c host.
Is just so used to it saying
at c host. (Imran laughs)
Anyway, okay, do ping that whatever is inside of cat hosts
and do go through the same scenario.
Now another thing, don't forget at the bottom
when you have do, what do you have to have?
Done.
Same thing if you have fi, you have to have, sorry,
if you have if, you have to have fi.
If you have do, you have to have done.
All right, perfect.
Now let's run it.
Let's verify we have it.
Okay, we do.
"Ping-script-all", hit enter.
"198.162.1.1 is OK"
and "192.168.1.235 is NOT OK".
You see how it works?
So, that's how you could actually create a script
that will have a multiple IP address
or multiple host name and that you wanted to ping.
And, of course, if you wanted to run this script
at a cron job, you could try that.
But we'll cover that later as well.
So, just focus on this one and try it.
And if you could seriously try to do it a cron job
that will automatically run at a certain time, try it.
It will work.
All right, good luck.