Jan 302012
 

Below script can be used to ping and telnet to different hosts running on different ports.

Things required:

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

techpaste.com:80
yahoo.com:80
google.com:443
gmail.com:443
noerror.com:81

 

Below is the bash script:

#!/bin/bash
#bash to check ping and telnet status.
#set -x;
#
#clear
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() {

SetParam
cat $URLFILE | while read next
do

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"
else
echo "$TIME : Status Of Host $server = $STATUS_DOWN" | mailx -s "$server Host DOWN!!!" $MAIL_TO

fi
done;
}

Telnet_Status() {

SetParam

cat $URLFILE | while read next
do

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";
else
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;

fi
done;
}
Main() {
Ping_Hosts
Telnet_Status
}
SetParam
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

Did Not Find The Exact Solution..? Put A Comment With Your Question TechPaste Support Will Get Back To You!!

  9 Responses to “Bash Script To Test Ping And Telnet To Different Hosts and Ports | *NIX”

  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

 Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>



Stats