Ping test in a shell script

#!/bin/sh
 
# -q quiet
# -c nb of pings to perform
 
ping -q -c5 google.com > /dev/null
 
if [ $? -eq 0 ]
then
	echo "ok"
fi

How it works

$? returns the exit status of the command previously executed. If ping is successful, $? will return 0. If not, it will return another number.

Feedback

Great! Thank you very much!
Danilo Castro de Souza
Jan 17, 2012
#1
It Helped;-) Thanks
Navid
Nov 30, 2012
#2
works !!! ... thanks :D
Ruwan
Dec 9, 2012
#3
#!/bin/bash

for i in $( cat $HOME/iplist )
do
ping -q -c2 $i > /dev/null

if [ $? -eq 0 ]
then
echo $i "Pingable"
else
echo $i "Not Pingable"
fi
done
Kalyan
Mar 15, 2013
#4
very good, I just don't understend the $? but I'll search... thanks!
brazil
Jul 27, 2013
#5
Thanks, worked like a charm.
Very useful.
Anonymous
Sep 2, 2013
#6
Awesome! Thank you.
Carlsbad, CA
Dec 17, 2013
#7
Thank you, both together:

#! /bin/sh
#
FILE=$HOME/iplist
#
#

if [ -n "$1" ]; then
ping -q -c2 $1 &> /dev/null

else
if [ -f $FILE ];
then
for i in $( cat $FILE )
do
ping -q -c2 $i > /dev/null

if [ $? -eq 0 ]
then
echo $i "Is_Pingable"
else
echo $i "Not_Pingable"
fi
done
exit 0

else
ping -q -c3 google.com > /dev/null
# <--- ping -q -c2 localhost > /dev/null
fi
fi

if [ $? -eq 0 ]
then
if [ -n "$1" ];
then
echo $1 Host_Found
else
echo "Google_Found"
fi

else
if [ -n "$1" ];
then
echo $1 Host_not_Found
else
echo "Google_not_Found"
fi
fi
Thanks
Jan 4, 2014
#8
I wrote this script to log the status of the internet ...

- - - 8< - - -

#address=192.168.1.99 # forced bad address
address=23.208.224.170 # www.cisco.com
internet=1 # default to internet is up

x=0

while true;
do
# %a Day of Week, textual
# %b Month, textual, abbreviated
# %d Day, numeric
# %r Timestamp AM/PM
echo -n $(date +"%a, %b %d, %r") "-- "
ping -c 1 ${address} > /tmp/ping.$
if [[ $? -ne 0 ]]; then
if [[ ${internet} -eq 1 ]]; then # edge trigger -- was up now down
echo -n $(say "Internet down") # OSX Text-to-Speech
echo -n "Internet DOWN"
else
echo -n "... still down"
fi
internet=0
else
if [[ ${internet} -eq 0 ]]; then # edge trigger -- was down now up
echo -n $(say "Internet back up") # OSX Text-To-Speech
fi
internet=1
fi
cat /tmp/ping.$ | head -2 | tail -1
sleep 30 ; # sleep: 60 seconds = 1 min
done

- - - 8< - - -
Michaelangel007
Apr 17, 2014
#9
Can you please help how can I execute the script, have to check ping response of IP's and google at particular interval of time
Jitender
Jun 26, 2014
#10
Does anyone know how to make a script to monitor the status of an application via the ping on a particular ip? For example using port 80, port navigation.
David
Sep 8, 2014
#11
Thank you

Jul 7, 2015
#12
I found this very helpful, However i wanted to just ping one address to make sure that the address is online before starting rdesktop. I found this very helpful, however i have changed it a little.:

REMOTE_ADDRESS=$(zenity --width="400" --height="100" --entry --title="Remote Desktop Connection" --text="Please enter the IP address:" --entry-text "192.168.1.5" );
if [ $? -eq 0 ] ; then
ping -q -c 2 $REMOTE_ADDRESS > /dev/null
if [ $? -eq 0 ]
then
rdesktoploop $REMOTE_ADDRESS;
else
alert warning "Unable to find Remote Computer.";
fi
fi
Ashley John
Dec 1, 2015
#13
Thanks from BCN!
BCN
Mar 10, 2016
#14
THANX !!
DavBook
Apr 19, 2016
#15
Thank you a lot!
Leonid
Aug 31, 2016
#16
excelent...thank so much
Richard Gerardo
Dec 16, 2016
#17
nice
nice
Jan 3, 2017
#18
Thank you!
Andreea
Feb 20, 2017
#19
great, thanks :-)
piny
Apr 7, 2017
#20
Danke für den Tip!
Klasse!
Uli Wahl
Sep 3, 2017
#21
Thanks
Tamil
Sep 5, 2017
#22
ping -c1 www.google.com > /dev/null 2> /dev/null; [[ $? -eq 0 ]] && echo OK! || echo "off!"
Helio
Nov 30, 2017
#23
Thanks Jérôme Jaglale!

P.S.:

$? variable has the value returned by last command executed. Usually, if the command was executed correctly, the returned value is zero. Then if zero make something...
AgOfEarth, from Brazil.
Jan 29, 2018
#24
Thank you AgOfEarth, that was not obvious. Fixed.
Jérôme Jaglale
Jan 30, 2018
#25