Shell Script : Check Ping To Remote Host And Port Opened

Attention: open in a new window. PDFPrintE-mail

Shell Script

#!/bin/bash

if [ "$#" = "0" ];
then
echo "Usage: $0 "
exit 1
fi

host=$1
port=$2
email=" This e-mail address is being protected from spambots. You need JavaScript enabled to view it "
subject="Script result"

if ping -q -c 4 $host >/dev/null
then

ping_result="OK"
else
ping_result="NOT OK"

fi

nc_result=`nc -z $host $port; echo $?`

if [ $nc_result != 0 ];
then
port_result="not opened"
else
port_result="opened"
fi

message="Ping to host - ${ping_result}, port $port ${port_result}."

if [ "$ping_result" != "OK" -o "$nc_result" != "0" ];
then
echo "$message"

echo "$message" | mail -s "$subject" $email

fi

Script Output

Ping to localhost and check is 22 port opened (ssh server)

bobbin@linoxide:/$ ./script 127.0.0.1 22
Ping to host - OK, port 22 not opened.
desktop:~/$

Learning Shell Script

#Check if service name passed to script as argument, if there no arguments (0) do next

if [ "$#" = "0" ];

then

#Write to terminal usage

echo “Usage: $0 ”

#Since no arguments – we need to exit script and user re-run

exit 1
fi

#Writing parameters to variables

host=$1
port=$2
email=” This e-mail address is being protected from spambots. You need JavaScript enabled to view it
subject=”Script result”

#Check if ping ok -q to quite mod, -c 4 for 4 checks

if ping -q -c 4 $host >/dev/null
then

#Next lines writes result variable

ping_result=”OK”
else
ping_result=”NOT OK”

fi

#Next command check if port opened via nc command, and getting exit status of nc command

nc_result=`nc -z $host $port; echo $?`

#Check of exit status of nc command, and write results to variables

if [ $nc_result != 0 ];
then
port_result=”not opened”
else
port_result=”opened”
fi

#Writing message that script will email and write to output

message=”Ping to host – ${ping_result}, port $port ${port_result}.”

#Next check if ping or port check is failed (ping if not OK and exit status of nc if not 0)

if [ "$ping_result" != "OK" -o "$nc_result" != "0" ];
then
echo “$message” #this line write warning message to terminal

echo “$message” | mail -s “$subject” $email #this line send email

fi