Jan 302012
Shell Scripting

Few times in administrative jobs we need to make sure some services or ports are always open and listening to monitor the same we can use below script can be used to ping and telnet to different hosts running on different ports. This bash script to ping multiple hosts can be used also to  telnet multiple hosts.

Things required:

Host_PortFile.txt – This file contains all the host names and port numbers needs to be pinged and telnet. Put your host names and ports like below and save it.



Below is the bash script:

#bash to check ping and telnet status.
#set -x;
SetParam() {
export URLFILE="Host_PortFile.txt"
export TIME=`date +%d-%m-%Y_%H.%M.%S`
export port=80
export STATUS_UP=`echo -e "\E[32m[ RUNNING ]\E[0m"`
export STATUS_DOWN=`echo -e "\E[31m[ DOWN ]\E[0m"`
export MAIL_TO="admin(at)techpaste(dot)com"
export SHELL_LOG="`basename $0`.log"

Ping_Hosts() {

cat $URLFILE | while read next

server=`echo $next | cut -d : -f1`

ping -i 2 -c 6 $server > /dev/null 2>&1

if [ $? -eq 0 ] ; then
echo "$TIME : Status Of Host $server = $STATUS_UP";
echo "$TIME : Status Of Host $server = $STATUS_DOWN";
echo "$TIME : Status Of Host $server = $STATUS_DOWN" | mailx -s "$server Host DOWN!!!" $MAIL_TO


Telnet_Status() {


cat $URLFILE | while read next

server=`echo $next | cut -d : -f1`
port=`echo $next | awk -F":" '{print $2}'`

TELNETCOUNT=`sleep 5 | telnet $server $port | grep -v "Connection refused" | grep "Connected to" | grep -v grep | wc -l`

if [ $TELNETCOUNT -eq 1 ] ; then

echo -e "$TIME : Port $port of URL http://$server:$port/ is \E[32m[ OPEN ]\E[0m";
echo -e "$TIME : Port $port of URL http://$server:$port/ is \E[31m[ NOT OPEN ]\E[0m";
echo -e "$TIME : Port $port of URL http://$server:$port/ is NOT OPEN" | mailx -s "Port $port of URL $server:$port/ is DOWN!!!" $MAIL_TO;

Main() {
Main | tee -a $SHELL_LOG

Above script will create a log with the script name like if the shell script is stored as shell.sh then the log name for this script will be shell.log, once you run the script. A sample output is given below

ping and telnet script sample output

  16 Responses to “Bash Script To Ping And Telnet multiple hosts”

  1. not work! get error " Can't exec /bin/bash at C:Perlbinpingtel.pl line 1."

  2. not work ,i get “Can’t exec /bin/bash at C:\Perl\bin\pingtel.pl line 1.”

    • Hi Issa,

      Check you have access to /bin/bash as this is a bash script and runs on UNIX systems.

      save it as pingtel.sh and RUN it from a UNIX machine.

  3. THANK YOU!!
    Big time save and a good base point to start a bigger script.

  4. How would you automate this script if there are multiple source hosts and multiple destination hosts. Logging into each source host to check if its destination hosts had necessary port is open would be time consuming. How to automate this situation.

  5. Excellent work, thanks

  6. Thankyou

  7. i just follow your script to be able put in my testing server anyway im able to ran manually the scrip but when i put in the crontab looks script its not working the log SHELL_LOG was not updated it

    • What was the error you are seeing while running the script. Can you try to give a absolute file name for SHELL_LOG

      export SHELL_LOG=”mylog.log”

      And Make sure you have “tee” in your PATH, commandline i.e. at /usr/bin/tee.

  8. Can you please explain this line ??

    TELNETCOUNT=`sleep 5 | telnet $server $port | grep -v “Connection refused” | grep “Connected to” | grep -v grep | wc -l`

    and what does grep -v grep mean ??

    • Hi Vicky,

      each five seconds it will check the count of Connected telnet sessions. grep -v grep is to remove the grep commands process from the list of outputs. You can check MAN pages of grep to find out more on -v option.


  9. I tried your script with host list having “Connection refused” in between of the input file.
    In your example you used “noerror.com:81″ at the end of file.

    The script comes out of the loop on encountering no connections and does not execute the remaining list of server:port

    Please let me know if i am missing anything here.

    • I just tried again with jumbling up the failed and working hosts its fine it does not skip.
      You are not seeing the status of the host as instead of printing the status down in console it sends a mail to admin stating the status is down for the host. you can just add one more line before below entry to let it print on console too. For convenience I have updated the same in the above article too just copy the script once again and check.

      echo “$TIME : Status Of Host $server = $STATUS_DOWN”;
      echo “$TIME : Status Of Host $server = $STATUS_DOWN” | mailx -s “$server Host DOWN!!!” $MAIL_TO

Leave a Reply