- [Instructor] In the previous video,
we looked at the ping command.
We'll now look at how to list all
the machines alive on a network.
In this video, we use two methods.
The first method uses ping,
and the second method uses fping.
We may need to check the availability
of other machines in the network.
A machine may not be alive in two conditions,
either it is not powered on,
or due to a problem in the network.
Shell scripting will help us report
which machines are alive on the network.
Let's see how to do it.
We'll be using ping and fping for this.
Fping is easier to use the script
and has more features as compared to the ping command.
Usually we only shoot to your Linux distribution by default,
so you'll need to manually install
it using your package manager.
I'm installing it now, here.
Let's go through the script to find out all
the live machines on the network,
and alternate methods to find out the same.
The first method.
We can write our own script using the ping command
to query a list of IP addresses
and check whether they are alive or not in this way.
Here we have used the ping command
to find out the alive machines on the network.
We used a fall loop for iterating through a list
of IP addresses generated using this expression.
The start, end notation will expand
and will generate a list of IP addresses
such as what is shown on the screen, up to 255.
This line will run a ping command
to the corresponding IP address
in each execution of the loop.
The -c option is used to restrict the number
of echo packets to be sent to a specified number.
This line is used to redirect both standard error
and standard out to 2 /dev/null,
so that it won't be printed on the terminal.
Using $?, we evaluate the exit status.
If it is successful, the exit status is zero,
else non-zero, hence the IP address
which applied to our ping are printed.
In this script, each ping command for the IP address
is executed one after the other,
even though all of the IP addresses
are independent of each other,
the ping command is executed as a sequential program.
It takes a delay of two echo packets and receiving them,
or the timeout for a reply
of executing the next ping command.
Okay, let's check the output.
Oops, permission denied, so sudo.
No, we need to use chmod.
Now let's see if we get the output.
Here, we're getting a list of machines
who are all alive, awesome.
Now, using fping, let's see the second method.
We can use an existing command line utility
to query the status of a machine
on a network using this command.
Or you can use this one.
I'm pausing the video now,
because it will take quite some time now.
We discussed a method for finding out the alive machines
on a network, let's see some enhancements,
and another method to do the same thing.
When it comes to 255 addresses,
the delay gets accumulated and becomes large.
We can run all the ping commands
in parallel to make this faster.
To make the ping commands run in parallel,
we enclose the loop body in brackets.
These brackets enclose a rack
of commands to run as a subshell,
and ampersand sends it to the background.
For example, in the for loop, we execute many background
processes and come out of the loop, terminating the script.
In order to prevent the script to terminate
until all its child processes end,
we have a command called wait.
Place wait at the end of the script
so that it waits for the time until all
the child subshell processes complete.
Using fping.
The second method uses a different command called fping.
It can ping a list of IP addresses simultaneously
and respond very quickly.
The options available with fping are the -a option,
where fping specifies to print
all alive machines' IP addresses.
The -u option with fping specifies
to print all unreachable machines.
The -g option specifies
to generate a range of IP addresses
from slash subnet mask notations,
specified as IP/mask or start and end IP addresses as...
2 > /dev/null is used to dump error messages
printed due to unreachable host to the null device.
It's also possible to manually specify a list
of IP addresses as command line arguments
or as a list through standard in.
For example, these commands.
Okay, awesome, so we discussed all the ways
of listing all the machines alive on a network.
In the next video, we'll show you how to run commands
on a remote host with SSH.