Hey, everyone, here is another newer script that we will go to.
It's about checking the other overconnectivity.
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 pangbourne.
Now, I know there are a lot of monitoring solutions that 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 the servers.
What are you going to do?
Is we actually going to create a script to check the status of the remote host or host and we'll find
out if it is pingle or not very simple script we actually have to combine in this group.
We have to combine, if else statements we might we will have to actually combine do while statement
together and you will see how we could ping the 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
black with you, I might forget and I might lose time.
And I definitely don't want to do that.
I definitely don't want to lose my time and your time.
So anyway, the first thing I want to do is I do want to mention one thing that previously we actually
created a directory called Professional Scripts.
Now I notice this whole directory is a very long directory.
Let's make it short.
So this way it would be easier to actually change direction from one place to another.
So I'm going to just do more professional directory scripts to piesse.
The symbols that now you'll see I have a direct to call piece and I go to P.S., 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 paying an
IP address.
Now, the IP address that I have selected is the IP address on my home modem, which is one to 168,
one out one.
And most likely, if you are in a home environment, not in the office, then your IP address is the
same as one eight one sixty one point one.
If it is not the same, then you could go find out the default gateway IP just to test if you copying
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 I one to 161 dot one.
This is my IP that I'm going to test.
So let's first create a script and I'm going to call the script paying.
OK, and this is the first time I will copy and then later on, so I'm just going to 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 status, OK, now let's get into the real
work now.
Do we define the variable now paying?
I'm sure you guys know what a pink script is, Pink 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 pink, then just typing one to 168, that one to one.
And you will see you are getting 64 byte back from 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 Penghu.
Minus C is count, just ping once.
The IP 192, once 168, won that one.
Now, as you go along, you 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 many times.
I just go online, I Google it and and I get the options.
So if I wanted to Google Ping only once, all typing, only once in Shell script or ping only once a
remote horse.
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 going to 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?
OK, we are trying to say is if.
Single bracket.
Dollar question mark now, what is a dollar question mark?
Dollar question mark is the output of all the command that you run by default.
The dollar question mark returns to, well, use one is zero, which is that.
Yes.
It it ran successfully or anything else, so these are the exit status, if you don't know the difference
between exit status, you can look it up.
But there are mostly two exit stated that I usually rely on exit status.
One answer.
So dollar question mark.
If the return of my command above, which is paying minus C one IP address is equal to zero.
OK, you see, if the return value is equal to zero, which means it did run, it was successful then.
Since the NFL stance statements, we have to specify then tab, as I said previously, always put tabs
so it will look neat and clean.
Then what do you want to do that, you know, the Penghu successful, the host is reachable.
And what do you want to 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?
Camping it, then what do you want to do then?
Is what else if that doesn't work, then what else else echo not OK?
Right, so if I do this or else do that.
OK, perfect.
Now every time you start, if you have to end it with.
The.
And that's pretty much it.
Save your script.
Give executable permissions to your script.
And now run the script, let's create a screen and I'll run run the script.
Perfect, you see, it ran and it ran one time and it came back and was successful, statistics came
back, one packet transmitted.
This is all part of pink script.
So we came back to active status was zero, which means it ran.
And that's why I said and that's why the script said it was OK.
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 going to change the IP address to one I to 61 to 35.
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 own a home network, your IP addresses are probably used from two to like 15 or 20 at
least, because we have so many gadgets at home.
But it's very highly unlikely that you probably are going to be at 235 IP.
So what I did is before I picked that IP, I just simply pinged that IP.
OK, you see, I pangu 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 Pingle either.
So I'm going to go into back into my script and this time I'm going to change the IP to instead of one
to one, I'm going to change it to two, three, five.
And save it now when I run it.
What should I get?
It can't get right and it's going to come back and it's going to say not OK.
So that's how the F else script works and to ping other host other devices, that is something that
you will definitely, definitely will be working on.
I'm sure if you are in a 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 going to give me that ping and then it will give me the nod.
OK, now what if I just I'm just concerned that don't show me anything.
Just tell me if it is OK and if it's not OK.
So what would we do here?
As I have written down here, you could output.
Because right here, when you do penk it output to the screen, if you do not want that to be output
on the screen, then there is this Syntex that you have to specify.
It's his ampersand Grayden sign.
And Deb, no, 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 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 going to go back into my script and this time I am going to tell it that, you know what?
Don't throw messages on the screen, I don't care about the message.
All I care about is what is the outcome?
Is it OK or is it not OK?
Now, I will run it.
Now, if you see, let me clear the screen so you will see it will come up very nice and neat and clean.
So he's not throwing all those messages.
All I'm going to get is not OK.
That's what I care about so many times.
You don't really want to 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're 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 want to cover is what if you want 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 to ping multiple times then you don't want to specify IP again and again
and again.
You just specify one time and then do the Hosta itself.
So I'm going to go back to into paing script.
And now I'm going to put in variable rate here and variable I'm going to do is host.
Equals sign code 192, 168, one to one, quote, close, that's my variable now, instead of paying
the IP, what I'm going to say is paying the host and dollar host.
Right, and this time I'm going to make my script a little fancier, I'm going to say if.
Paying this host, which is this this variable, and I'll put two additional, if the output is successful,
then OK, instead of just OK, I'm going to say then say hosts the IP is OK, is OK.
And else, if that if he's not Pingle, then host.
Is not OK.
You see, I used 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 have specified the IP just one time as a variable right here and now I'm just going to use that variable
as a or that container and I'm going to use it again and again and again.
OK, so let's run it.
Nice, right IP that I'm trying to ping is OK.
So now it's very specific that's telling me that this is the IP that you're trying to ping and it is
OK not to worry about it now.
What if I change the IP to 235?
She is, of course, one of my that is not payable that is not a sign and now let's clear the screen
so we could see better paying scrip.
It's going to ping.
And the reason it takes a little longer, because sometimes Peng just waits for it to respond.
It's by default.
It just waits for it anyway.
So, you see, it came back.
It says one eight to 161 out to thirty five is not OK.
Perfect.
See how we actually make the use of variable here and be used if and L statements and we are putting
the variables in place.
OK, now next one is changing.
IP already did that.
Perfect.
Now the next one is a little complicated one, but it's easy if you do it together, ok.
Now what if you wanted to ping multiple IP address in one script.
OK, one ip I kapanga.
Right I could.
I don't want to.
I have to create a script hoping that right now I understand you're probably thinking that.
But the reason we start off with one IP, so you would understand what if you wanted to pay 100 servers.
Now things get interesting.
Now you really need didn't have that script.
Right.
OK, so first of all, what you need to do is you need to put that IP addresses that you want to paying
to a file.
So I'm going to create a file and I'm going to call it
my hosts.
And I'm going to put in the IP address of one to 168 at one to one and the second one, I'm going to
put it one way to one, 68, one, two thirty four.
So now, since we are working on only two ipis, if you are multiple because you want to try it.
Please go ahead now.
Safe.
Now this is just an empty file.
Sorry, this is just a flat file.
And let's do my host 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 going to create a new script from scratch, but I'm going to do is I'm going to
just copy my paing script and I'm going to name it Pangu script dash all.
So this way it will identify that this is for all multiple and service multiple holes.
OK, now the shipping script, Dasch, all you see everything here is done, but I just have to change
the description says this will ping a remote host, not a more host, multiple remote hosts.
Make sure you change this description is very important.
OK, now we're going to come in and what I'm going to do is I'm going to simply you know what?
Let's first get rid of this variable host.
I'm going to start from here.
What are those host?
So this is the variable and variable is what you have to specify the location of your file.
So that is Egholm.
I saw Flash PSP for perfection scripts.
Remember, we did that and B, what was the name we picked for the flash file.
Was it hosts.
Not sure actually.
Let me go check, so please make sure this location and this host file is correct.
Otherwise the script is going to complain.
No, I specified my host and it is home, I have a p e directory.
My host.
OK, perfect.
So now here is the location of the file that has all my multiple host.
OK, perfect.
Now I wanted to use for loop for that.
Why?
Because for Loop allows you to go through the script once or twice or as many times until everything
is being read or everything has been satisfied.
So if I don't specify for Loop, what will happen is it will pick the first IP in the list and it will
be done.
It's not going to 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 Darla.
Parentheses in where cat?
Specify.
Dollar hosts.
OK, now you see what I did for i.p, I'm putting I'm creating a container, another container ipe in
and I'm carrying this at sea.
Horsfall what is sorry, not as I'm carrying this.
Darla Horseflies, What is Dollar Horsfall, Dollar Horsfall is the.
Home, I was all smiles, so this is what I'm telling for IP and put everything that comes from this
result cat host put everything in it.
All right, perfect.
I will do that.
Now, what else 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 ok.
So for ok.
Now what do you want me to do.
I'm going to do is I'm going to move this one tab over and I'm going to say ping C1, same thing.
And now instead of host I wanted to do.
IP, because right here.
This is IPY, this is the container, I'm specifying it right here.
Now you're probably thinking, hey, Amron, why can't I just use the same variable?
Hmm, that's a good question.
I don't know.
Tried maybe it works, but this is how I actually go through my practice.
And if you want to try it, please be my guest.
Try it if it works.
Perfect.
All right.
So, OK, going into debt.
Not now.
Coming back to our same script if.
Pound a dollar question mark equals zero, then instead of hose.
Now you're talking about IP, IP is I put it there paying pass.
I'll just 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.
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 were writing a script, many times
I would say even 80 percent or 85 percent of times you make mistakes.
I make mistakes, too.
That's why I try to write it.
So there won't be any mistakes.
OK, so this is how I was going to do again.
So I hopefully you understand what I'm trying to get at.
This is the variable and what variable is going to do?
It's going to specify the name of the file.
OK, now for IP, this is another variable and is saying that, you know what, go and read that file
cat that file and that file is that's just Donahoe's.
I don't know why I keep saying that Seahorse is just so used to it saying it's seals anyway.
OK, do ping that whatever is inside of Carse cat house and 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 fee.
You have to have so if you have if you have to have fee, if you have, do you have to have done.
All right, perfect.
Now let's run it.
Let's verify we have it, OK?
We do ping script all hit, enter one, ited 162, one one is OK and 192 161 that 235 is not OK.
You see how it works.
So that's how you could actually create a script that would have a multiple IP address or multiple hostname
and that you want to ping.
And of course if you wanted to run the script at a cron job, they could try that, but we'll cover
that later as well.
So just focus on this one and try it.
And if you if you could seriously try to do it, cron job that will automatically run a certain time.
Try it.
It will work.
All right.
Good luck.