Hey everyone there is another newer script that will go where 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
Pangle.
Now I know there are a lot of pet monitoring solutions that are out there that a lot of companies have.
They actually monitor the servers they monitor the health check and everything but still once in a while
you do need to actually paying 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 we 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 Pangle or not.
Very simple script.
We actually have to combine in discovery you have to combine if else statement view we will have to
actually combine do wild statements together and you will 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 were completely
black with you 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 noticed this whole directory is a very long directory lets make it short.
So this way it will be easier to actually change it from one place to another so I'm going to just do
move professional directory scripts to P.S. simple as that.
Now you'll see how it called PSN 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 group
that is going to paying an IP address.
Now the IP address that I have selected is the IP address of my home modem which is one to 168 one one
and most likely if you are in a home in wireman not in the office then your IP address is the same as
when I was 61.
What 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 paint or you could just pick any IP that you could test your script against.
So I have picked one to $161 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 paing.
OK.
And this is the first time I will copy later on so I'm just going to quickly do the information
day to
description.
This script will hang a remote host and notify of course then
modified.
And since this is the first time and I am quite used to yes.
OK.
Now let's get into the real work now.
We define the variable now.
Tang 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 say right and quit out of this script.
Make sure you save it and just if you don't know ping then just typing one to 168 one to one and you'll
see you are getting 64 byte back from the 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 wanted to panga minus C is count just pinged once
the IP 1 to 1 168 1.1.
Now as you go along you're probably thinking Hey how come Emraan knows all those different options to
specify how 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 I get the options.
So if I wanted to Google paying only once I'll type Pengelley ones in shellscript or Pengelley once
a remote host and it will give me the option that I'm looking for.
So don't worry about it.
You're you're not the one 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.
I'm moving forward.
So we are pinging this IP All right it will ping 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 ran successfully.
Or anything else.
So these are the exit status.
If you don't know differences between exit status you can look it up but they are mostly to exit data
that I usually rely on existe that 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 we.
If else then 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 pingers are successful the host is reachable.
What are you gonna do.
Well simply say OK.
How's that.
Just simple.
All right.
Perfect.
Now what if you don't get the zero status which is campaign.
Then what do you wanna do then.
What else if that doesn't work then what else else.
Eckel not OK.
Right.
So if do this else do that OK.
Perfect.
Now every time you start if you have to end it.
The and that's pretty much it save your script give an executable permissions to your script and now
run the script let's create a screen and now run 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's script.
So we came back the exit status was zero which means it's right.
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 going to change the IP address 2 1 to 1 61 to 35.
Now how do I get that IP address I just paying some of the IP addresses in my home network.
I'm sure if you are in a home network your IP addresses are probably used from two to like 15 or 20
at least because you have so many gadgets at home.
But it's very highly unlikely that you're probably going to be at 235 IPs.
So what I did is before I picked that IP I just simply pinged that IP OK.
You see I ping that IP and that IP is not reachable.
So that's why I just picked that IP.
You can pick any IP that is not Pangle 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
one I'm going to change it to two three five and save it.
And now when I run it what should I get.
It Camping's it right and it's going to come back and it's going to say not OK.
So that's how the if else script works.
And to panga other host of 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 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 OK and if it's
not OK.
So what do we do here.
As I have written down here you could output cars right here and you do PANK it output to the screen
if you do not want to be output on the screen.
Then there is this syntax that you have to specify it's his ampersand 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 Dev-Null.
It was 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 pink 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 OK or is it not OK.
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.
So he's not doing 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 will 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 want to Khowar 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 straight IP you paying the variable.
Why do I do that.
Because many times you do wire to add multiple hosts or you know certain things that you want to.
You probably wanted 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 the host do itself.
So I'm going to go back into ping script and now I'm going to put in variable right here and variable.
I'm going to do is host
equal sign code 192 168 out Juanda one called closed 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 does this variable and output to a Dev-Null if the output
is successful then OK instead of just OK I'm going to say then say host the IP is ok is OK an else if
that is not Pangle then oh is not ok you'll see I used my variables 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.
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 paying is OK.
So now it's very Pacific that is telling me that this is the IP that you trying to paing and it is ok
not to worry about it.
Now what if I change the IP to 235
which is of course one of my P that is not Pangle that is not assigned.
And now let's clear the screen so we could see better pings script.
It's going to ping.
And 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 anyway.
So you see it came back it says one to 161 out to thirty five is not OK.
Perfect see how we actually make use of variable here and use if else statements and we are putting
the variables in place again.
Now next one is changing IPV already did that.
Perfect.
Now the next one is a little complicated one but it's easy if we do it together.
OK.
Now what if you wanted to ping multiple IPX in one script.
OK.
One IP I Kapanga right.
I don't even have to create a script to paying 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 were paying hundreds or worse.
Now things get interesting.
Now you really need to have that script right.
So first of all what you need to do is you need to put that IP address is that you want to.
Paying to a file.
So I'm going to create a file and I'm going to call it
my hopes and I'm to put in the IP address of 1 8 to 168 to 1 1 and the second one I'm going to put one
to 168 1 235.
So now since we are working on only two IPs if you have multiple IPs you try please go ahead.
Now safe.
Now this is just the empty file.
Sorry sergeant is just a flat file.
And let's do my host and you'll see it housed 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 what I'm going to do is I'm going I
just copy my pink script and I'm going to name it hang script dash all.
So this way it will identify that this is for all multiple servers multiple host.
OK.
Now the paying script dash all you see everything here is done but I just have to change the description
it says this quote paying a remote host not more host muthas all 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 started 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 slash home.
I saw slash P.S. P.S. for professional scripts remember we did that.
And B what was the name we picked for the flat file.
Was it host.
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 going to complain.
No I specified my host and its home.
So p s.
My hosts
OK perfect.
So now here is the location of the file that has all my multiple hosts.
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 for you 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 a 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 dollar parentheses in where cat specify
dollar horse
OK now you see what I did for IP I'm putting I'm creating a container in another container IP in and
I'm catting this at seahorse for what is.
Sorry not ASIO's what I'm catching this Darkhorse files what is dollar host file a host file is the
home I have.
P.S. My host so this is what I'm telling for IP and put everything that comes from this result cat host
put everything in IP.
All right perfect.
I'll do that.
Now what else you Ahmed now do.
Because you have to specify what exactly once you do for always has to have a do for do.
Done for Wilda.
OK.
So for OK now are you do.
I'm going to do is I'm going to move this one tablet or I'm going to say paying C-1 same thing and now
instead of host I want to do IP because right here this is IP right.
This is the container I'm specifying it right here.
Now you're probably thinking Hey Emraan Why can't I just use the same variable.
Hmm that's a good question.
I don't know I tried.
Maybe it works.
But this is how I actually go to my practice.
And if you want to try it please be my guest.
Try it if it works.
Perfect.
All right.
So IP OK going into debt now.
Now coming back to our same script if pound dollar question mark equals zero then and said home.
Now we're talking about IP IP.
As I put it there.
Pink pass.
I'll just keep leave it as his 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're 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 he's going to do again.
So hopefully you understand what I'm trying to get at.
This is the variable and the variable is going to do.
It's going to specify the name of the file.
OK.
Now for IP This is another variable and it's saying that you know what.
Go and read that file of cat that file and that file is at sea just dollar.
I don't know why I keep saying the host is just so used to the host anyway.
OK.
Do paying that.
Whatever is inside of Cass can coast 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.
That's verified we have it.
Can you do paing script or hit enter.
When I did 162 one one is OK.
And 192 161 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 hostname
and that you want to ping.
And of course if you wanted to run this script at a cron job you could try that but will code later
as well.
So just focus on this one and try it.
And if you if you could seriously try to do it a cron job that will automatically run a certain time
try it will work.
Good luck.