Learn Linux via Shell Scripting: Ping Sweep

Learn Linux via Shell Scripting: Ping Sweep

Several friends mentioned recently that they wanted to learn Linux and asked whether any books and/or videos I'd recommend. I don't really read well.

Deploy Scalable and Reliable WordPress Site on LEMP(2)
Deploy Scalable and Reliable WordPress Site on LEMP(3)
Deploy Scalable and Reliable WordPress Site on LEMP(1)

Several friends mentioned recently that they wanted to learn Linux and asked whether any books and/or videos I’d recommend. I don’t really read well. I don’t watch videos much either due to it is a bit time-consuming.

My learning approach includes knowledge breakdown, purpose and the combination of theory and practice. Linux, for example, is a very broad topic. What should be my focus areas and the relevant areas (knowledge breakdown)? Why should I learn – how important to my work and/or interest (purpose)? Create the opportunity to put the knowledge into practice (the combination of theory and practice).

I tend to use books and videos as references to answer particular questions or provide a knowledge overview – the table of content, chapter summary and etc.

We will learn some basic Linux concepts via a simple BASH script, Ping Sweep, in this lab.

What is Shell?

Shell interprets the end user commands to allow communication with the operating system kernel (Figure 1). Shell provides a command-line interface (CLI) serving a similar purpose to graphical interfaces as in Windows and Mac OS.

Figure 1: What is Shell?

What is BASH?

There are various types of Shell. BASH stands for Bourne Again Shell, the free version of the Bourne Shell distributed with Linux. Bourne Shell is the original UNIX shell and named after its developer Stephen Bourne, with the file extension “.sh”.

Ping Sweep BASH

I developed a Ping Sweep BASH on my Mac; though any Linux OS can be used. Some useful commands to understand your system environment are as below. Comments and explanastion are marked with #.

I created a file ‘ping_scan.sh’ under ‘/bin/’ by executing the following command.

‘sudo’ stands for super user do, which allows me to execute commands/tasks with administrative permission. nano is a terminal text editor. vim is another common editor, though nano is easier to use.

Input the following script in the ‘ping_scan.sh’ file.

#!/bin/bash declares the script will be BASH shell. Remember we mentioned earlier that there are various types of shell.

PATH= and export PATH defines the script running environment.

for each i in…do defines variable ‘i’ and create a loop. For each i between 192.168.1.1 and 192.168.1.255 do the following. ‘do’ indicates the beginning of the loop; while ‘done’ indicates the end of the loop.

ping -c 1 $i ping the current IP address in the variable $i once. Make sure you understand the manual commands/configuration before any automation. In our case, ensure ping -c 1 192.168.1.1 work.

> /dev/null  means output to ‘/dev/null’. /dev/null is a dump yard so that unwanted information will not show on the screen. You will see the following output on the screen without ‘> /dev/null’.

With ‘> /dev/null’, you will see the following output, neat and tidy:

[ $? -eq 0 ]  compares the ping result return ($?) with 0. The return value 0 means success. Please note the space between [ and $, as well as 0 and ].

&& A && B means if A condition is met then execute B; if A condition is not met, then don’t execute B.

echo “Node with IP $i is UP.”  If ping is successful, then print Node with IP 192.168.1.x is UP.

|| either or condition. A || B means if A condition is met, then don’t execute B; while if A condition is not met, then execute B. The shell scripts always execute from left to right.

|| echo “Node with IP $i is DOWN.” In the case of unsuccessful ping, print Node with IP 192.168.1.x is DOWN.

done ‘do’ indicates the beginning of the loop; while ‘done’ indicates the end of the loop.

If different command components are in the same line, we use ; to separate.

We now test run our BASH script. -x allows debug mode as below. If the script doesn’t work well, you will know where the hiccups are.

The final output will look like below:

COMMENTS

WORDPRESS: 0
DISQUS: 0