View Full Version : Test if host is 'up'?
4th July 2004, 02:20 PM
I'd like to write shell scripts which execute stuff *only* if a host is 'up'.
How do i test this in a script?
4th July 2004, 02:23 PM
Perhaps you could use dig and filter the response for the desired value?
4th July 2004, 02:33 PM
are you talking about host over a network? I am not a bash shell script expert but if you know the syntax, you could use a ping and grep command like this:
ping -c 4 google.com | grep "0 received"
if nothing (null/false) is returned, the host is reachable. of course this would be in an if statement.
It is quite crude, but it might just work. hopefully someone with more scripting knowledge will join this thread.
4th July 2004, 02:39 PM
thank you all for the quick replies,
using a custom ping timeout is even better:ping -c1 -w2 192.168.0.127 | grep "0 recei"
but the question is *how* do i use this in the 'if' statement?
EDIT: http://www.lysator.liu.se/~forsberg/linux/shell-scripts.html had the answer!
[root@woodstock share]# cat ./is_up.sh
ping -c1 -w2 192.168.0.126 > /dev/null
if [ $? != 0 ] ; then
echo "host is down"
echo "host is up"
thank you guys.
13th July 2004, 02:32 PM
Moved to Programming.
13th July 2004, 03:49 PM
is there a service running on the host? if so how about using nmap to probe the port for that service and if it returns "open" then set your host up variable to true
nmap -p22 hostname
will probe if ssh is listening
14th July 2004, 11:58 PM
Thanks for the hint, Varkk. Maybe I'll need that someday.
16th July 2004, 10:31 AM
You can try fping. "fping is meant to be used in scripts and its output is easy to parse". I use it to ping test for lstat statistics. You can download it from http://www.fping.com/ (I saw RPM somewhere).
17th July 2004, 12:21 AM
That looks very promising, too. Thanks for that.
vBulletin® v3.8.7, Copyright ©2000-2013, vBulletin Solutions, Inc.