Shell Script : Check Ping To Remote Host And Port Opened
This shell script enables you to perform a ping to remote host and check whether the mentioned port is opened on that server. This helps system admin for ping test and also make sure if there any issues with specific ports.
Shell Script
Test Ping And Open Port
#!/bin/bash
# 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=”test@expertslogin.com”
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 variableping_result=”OK”
else
ping_result=”NOT OK”fi #end of fi loop
#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 #exit of fi loop#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 terminalecho “$message” | mail -s “$subject” $email #this line send email
fi
Ping to localhost and check is 22 port opened (ssh server)
desktop:~/$ ./script 127.0.0.1 22
Ping to host – OK, port 22 not opened.
desktop:~/$
Category: HOWTOS, LINUX SHELL SCRIPT





