From Vladimir Kukuruzovic on the Linux Users Support Team mailing list on 20 Sep 1998
Hi, regarding to your answer guy message
Conditional Execution Based on Host Availability
From the L.U.S.T Mailing List on 07 Aug 1998
#!/path/to/perl $ping = Ping -c 1 10.10.10.10; exec ("program") if $ping =~ /100\% packet loss/;
What's wrong with a simple:
ping -c 1 $target && $do_something $target || $complain
... where you fill $do_something and $complain with commands that you actually want to run on success or failure of the 'ping'.
That's what shell "conditional execution operators" (&& and ||) are for after all.
your program does not work well with current release of net-tools and ipv6
support.
you should rewrite it this way:
ping -c 1 -q $target 2> /dev/null | fgrep "1 packets received" \
/dev/null && $do_something $target || $complain
This doesn't look right to me. My example simply
sends a ping packet and tests the return value.
It's possible that this host might not be reachable
by some ping's (ICMP's) --- that there might be
some lossage. However, I was just giving the simple
case of a "well-connected" system on the local LAN.
I should not have to use 'grep' and parse the output
from the ping command. It should return an error level
that reflects the results.
If it doesn't do that in some new release --- I'll
hack it back in myself. (Ideally it might offer an
option to specify a threshold lossage percentage
--- on which it returns an error. But adding a
command line option to 'ping' for this might be
"gilding the lily" --- and adding anything to it
(since it is, by nature, an SUID program) is a
unpleasant prospect.
kind regards,
Vladimir
p.s. the original program would say that everything is ok when
$target is in DNS, but is not reachable (no route to host)
By that I presume you're referring to the fragment
of perl code. Mine did not seem to do this (since
I tested it with several degenerate cases).
Copyright © 1998, James T. Dennis
Published in Linux Gazette Issue 33 October 1998
![[ Answer Guy Index ]](../../gx/dennis/answernew.gif)
floppy
autocad
scsi
samba_pdc
virthost
emacs_cc
ipmasq
tty
shuffle
connect
hostavail
desqview
catch22
thanks2
typo