Simple Bash Script for Pinging Hosts
This is a simple useful script which I learned from “Coding For Penetration Testers” book. It is used to check if a host is alive or not and echoes on user screen a message with the results of the ping command, if the host is up it informs the user “HOST IS UP” with the echo command like shown in Figure 1.
We use the if conditional to test if the host is up and print to the screen a message with the echo command, if the host is not alive we print “HOST IS DOWN”, use an else statement to do it. This is the heart of our script, and it is inside a function called pingcheck. As you can see from Figure 1 we declare a function called pingcheck with the function command. After that, we open and close curly brackets and put our code inside it. The ping variable holds the result of the piping process inside backticks. We use ‘ping -c 1′ to ping our target with a single packet and pipe the result of this command with a pipeline to the grep command and grep the word bytes. After that, we use piping again and send the result of grep command to wc command which stands for word cound. The final result of these commands stringed together is stored in the ping variable which we test in the if statement. The ping variable holds a number, which is the number of word ‘bytes’ returned after ‘ping -c 1 $host’ command is executed. If the value of ping variable is greater than 1 it means that the host has responded to our request, which means the host is alive. The ‘fi’ command inside pingcheck function indicates the end of the if statement. To execute the pingcheck function, we write ‘pingcheck’ outside the function. The first line of this script is the shebang line, which tells the system what interpreter to use in order to interpret the script. The second line is used to get input from the user and $1 means the first argument. To execute this script, open a terminal and type ‘./ping.sh host’, where ping.sh is the Bash script and host is the first argument. For example run ‘./ping.sh www.google.com’ like shown in Figure 2.
The output is shown in Figure 3.
Like us on Facebook
This week Top Posts
- Top Things To Do After Installing Ubuntu 13.10 'Saucy Salamander' : Ubuntu 13.10 Saucy Salamander will be released on coming October 17th with many new salient featur...0 comments |
- Configure Your Browser To Use Tor On Ubuntu/Debian/Linux Mint : Tor, The Onion Router, is a network of Virtual Tunnels that allows users to communicate securely and...2 comments |
- Setup A Full Featured ITIL Management System Using Integria IMS On CentOS 6 : Integria IMS is a fully featured ITIL management system, featuring a ticketing system, inventory/C...2 comments |
- How To Upgrade From Ubuntu 13.04 Raring To Ubuntu 13.10 Saucy Salamander : Ubuntu 13.10 Saucy will be released on October 17th. Hope it will come with lot of improvements and ...0 comments |
- Install lamp with 1 command in Ubuntu 12.10, 13.04 Raring Ringtail & LinuxMint13 : Updated: 10/09/2012 :LAMP (Linux, Apache, MySQL and PHP) is an open source Web development platform ...0 comments |
- Install and Configure Samba share in Ubuntu 13.10 'Saucy Salamander' , 13.04| Howto : Updated 05-04-2013: One of the most asked features for Samba is a graphical user interface to help w...0 comments |
- Configure Your Browser To Use Tor On Ubuntu/Debian/Linux Mint
- Setup A Full Featured ITIL Management System Using Integria IMS On CentOS 6
- Install LibreOffice 4.1.3 in Elementary OS ‘Luna’
- How To Install Simple Scan in Crunchbang ‘Waldorf’
- Selene Media Encoder: Convert Audio, Video Files To Most Popular Formats
- Linux Kernel 3.12.2 Is Available For Download! Install / Upgrade Instructions
- CentOS 6.5 Has Been Officially Released!
- Format Junkie: Convert Media Files To All Popular Formats
- Setup Squid Proxy Server On openSUSE 13.1
- TimeShift: Restore Your Linux Desktop To Previous State
This work by unixmen.com is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.
Copyright © 2008-2013 Unixmen.com .