PDA

View Full Version : Test if host is 'up'?



pigpen
4th July 2004, 02:20 PM
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
4th July 2004, 02:23 PM
Perhaps you could use dig and filter the response for the desired value?

superbnerd
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.

pigpen
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
#!/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
13th July 2004, 02:32 PM
Moved to Programming.

Varkk
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
e.g:
nmap -p22 hostname
will probe if ssh is listening

pigpen
14th July 2004, 11:58 PM
Thanks for the hint, Varkk. Maybe I'll need that someday.

Szpak
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).

Szpak

pigpen
17th July 2004, 12:21 AM
That looks very promising, too. Thanks for that.