View Full Version : Test if host is 'up'?
pigpen
2004-07-04, 07:20 AM CDT
Hi all,
I'd like to write shell scripts which execute stuff *only* if a host is 'up'.
How do i test this in a script?
mark
2004-07-04, 07:23 AM CDT
Perhaps you could use dig and filter the response for the desired value?
superbnerd
2004-07-04, 07:33 AM CDT
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.
pigpen
2004-07-04, 07:39 AM CDT
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
#!/bin/sh
ping -c1 -w2 192.168.0.126 > /dev/null
if [ $? != 0 ] ; then
echo "host is down"
else
echo "host is up"
fi
thank you guys.
Jman
2004-07-13, 07:32 AM CDT
Moved to Programming.
Varkk
2004-07-13, 08:49 AM CDT
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
e.g:
nmap -p22 hostname
will probe if ssh is listening
pigpen
2004-07-14, 04:58 PM CDT
Thanks for the hint, Varkk. Maybe I'll need that someday.
Szpak
2004-07-16, 03:31 AM CDT
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).
Szpak
pigpen
2004-07-16, 05:21 PM CDT
That looks very promising, too. Thanks for that.
vBulletin® v3.8.1, Copyright ©2000-2009, Jelsoft Enterprises Ltd.