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.



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

People Searching On This Page:
  • telnet javascript

Did Not Find The Exact Solution..? Put A Comment With Your Question, Our Expert Tech Members Will Get Back To You!!

  14 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

  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.


 Leave a Reply



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>