191 lines
5.9 KiB
HTML
191 lines
5.9 KiB
HTML
<!--startcut ======================================================= -->
|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
|
|
<html><head>
|
|
<META NAME="generator" CONTENT="lgazmail v1.1preC">
|
|
<TITLE>The Answer Guy 33:
|
|
Conditional Execution Based on Host Availability
|
|
</TITLE>
|
|
<!-- ORIGINAL SUBJECT:
|
|
Conditional Execution Based on Host Availability
|
|
JTD SUBTITLE:
|
|
|
|
-->
|
|
</head>
|
|
|
|
<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#A000A0"
|
|
ALINK="#FF0000">
|
|
<H4>"Linux Gazette...<I>making Linux just a little more fun!</I>"
|
|
</H4>
|
|
<P> <hr> <P>
|
|
<!-- ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: -->
|
|
<H1 align="center"><A NAME="answer">
|
|
<img src="../../gx/dennis/qbubble.gif" alt="" border="0" align="middle">
|
|
<a href="../index.html">The Answer Guy</a>
|
|
<img src="../../gx/dennis/bbubble.gif" alt="" border="0" align="middle">
|
|
</A></H1>
|
|
<BR>
|
|
<H4 align="center">By James T. Dennis,
|
|
<a href="mailto:linux-questions-only@ssc.com">linux-questions-only@ssc.com</a>
|
|
<BR>Starshine Technical Services, <A HREF="http://www.starshine.org/">http://www.starshine.org/</A>
|
|
</H4>
|
|
<p><hr><p>
|
|
<!--endcut ========================================================= -->
|
|
<H3><img src="../../gx/dennis/qbub.gif" alt="(?)"width="50" height="28"
|
|
align="left" border="0">Conditional Execution Based on Host Availability</H3>
|
|
<p><strong>From Vladimir Kukuruzovic on
|
|
the Linux Users Support Team mailing list
|
|
on 20 Sep 1998 </strong></p>
|
|
<!-- begin body -->
|
|
|
|
<p><strong>
|
|
Hi, regarding to your answer guy message
|
|
</strong></p>
|
|
|
|
|
|
<font color="navy"><em>
|
|
<p><strong>
|
|
Conditional Execution Based on Host Availability
|
|
</strong></p>
|
|
|
|
<p><strong>
|
|
From the L.U.S.T Mailing List on 07 Aug 1998
|
|
</strong></p>
|
|
|
|
<pre><strong>#!/path/to/perl
|
|
$ping = Ping -c 1 10.10.10.10;
|
|
exec ("program") if $ping =~ /100\% packet loss/;
|
|
</strong></pre>
|
|
|
|
<p><strong>
|
|
What's wrong with a simple:
|
|
</strong></p>
|
|
|
|
|
|
<p><strong><code>
|
|
ping -c 1 $target && $do_something $target || $complain
|
|
</code></strong></p>
|
|
|
|
|
|
|
|
<p><strong>
|
|
... where you fill $do_something and $complain with commands that you
|
|
actually want to run on success or failure of the 'ping'.
|
|
</strong></p>
|
|
|
|
|
|
|
|
<p><strong>
|
|
That's what shell "conditional execution operators" (<tt>&&</tt>
|
|
and <tt>||</tt>) are for after all.
|
|
</strong></p>
|
|
</em></font>
|
|
|
|
|
|
<p><strong>
|
|
your program does not work well with current release of net-tools and ipv6
|
|
support.
|
|
<br>you should rewrite it this way:
|
|
</strong></p>
|
|
|
|
|
|
<p><strong><code>
|
|
ping -c 1 -q $target 2> /dev/null | fgrep "1 packets received" \
|
|
<br> /dev/null && $do_something $target || $complain
|
|
</strong></p>
|
|
|
|
|
|
<blockquote><img src="../../gx/dennis/bbub.gif" height="28" width="50"
|
|
alt="(!)" border="0"
|
|
>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 <EM>some</EM> 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.
|
|
</blockquote>
|
|
|
|
|
|
<blockquote>
|
|
I should <EM>not</EM> have to use '<tt>grep</tt>' and parse the output
|
|
from the ping command. It should return an error level
|
|
that reflects the results.
|
|
</blockquote>
|
|
|
|
|
|
<blockquote>
|
|
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 '<tt>ping</tt>' for this might be
|
|
"gilding the lily" --- and adding anything to it
|
|
(since it is, by nature, an SUID program) is a
|
|
unpleasant prospect.
|
|
</blockquote>
|
|
|
|
|
|
|
|
<p><strong><img src="../../gx/dennis/qbub.gif" height="28" width="50"
|
|
alt="(?)" border="0"
|
|
>kind regards,
|
|
<br>Vladimir
|
|
</strong></p>
|
|
|
|
<p><strong>
|
|
p.s. the original program would say that everything is ok when
|
|
$target is in DNS, but is not reachable (no route to host)
|
|
</strong></p>
|
|
|
|
|
|
<blockquote>
|
|
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).
|
|
</blockquote>
|
|
<!-- end body -->
|
|
<!--startcut ======================================================= -->
|
|
<P> <hr> <P>
|
|
<H5 align="center"><a href="http://www.linuxgazette.com/copying.html"
|
|
>Copyright ©</a> 1998, James T. Dennis <BR>
|
|
Published in <I>Linux Gazette</I> Issue 33 October 1998</H5>
|
|
<P> <hr> <P>
|
|
<!--::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::-->
|
|
<table width="98%"><tr valign="center" align="center">
|
|
<td rowspan="3"><A HREF="../lg_answer33.html"><IMG
|
|
SRC="../../gx/dennis/answernew.gif"
|
|
ALT="[ Answer Guy Index ]"></A></td>
|
|
<td><A HREF="floppy.html">floppy</a>
|
|
<td><A HREF="autocad.html">autocad</a>
|
|
<td><A HREF="scsi.html">scsi</a>
|
|
<td><A HREF="samba_pdc.html">samba_pdc</a>
|
|
<td><A HREF="virthost.html">virthost</a>
|
|
</tr><tr valign="center" align="center">
|
|
<td><A HREF="emacs_cc.html">emacs_cc</a>
|
|
<td><A HREF="ipmasq.html">ipmasq</a>
|
|
<td><A HREF="tty.html">tty</a>
|
|
<td><A HREF="shuffle.html">shuffle</a>
|
|
<td><A HREF="connect.html">connect</a>
|
|
</tr><tr valign="center" align="center">
|
|
<td><A HREF="hostavail.html">hostavail</a>
|
|
<td><A HREF="desqview.html">desqview</a>
|
|
<td><A HREF="catch22.html">catch22</a>
|
|
<td><A HREF="thanks2.html">thanks2</a>
|
|
<td><A HREF="typo.html">typo</a>
|
|
</tr></table>
|
|
<P> <hr> <P>
|
|
<!--::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::-->
|
|
<A HREF="../index.html"><IMG SRC="../../gx/indexnew.gif"
|
|
ALT="[ Table Of Contents ]"></A>
|
|
<A HREF="../../index.html"><IMG SRC="../../gx/homenew.gif"
|
|
ALT="[ Front Page ]"></A>
|
|
<A HREF="../lg_bytes33.html"><IMG SRC="../../gx/back2.gif"
|
|
ALT="[ Previous Section ]"></A>
|
|
<A HREF="../vrenios.html"><IMG SRC="../../gx/fwd.gif"
|
|
ALT="[ Next Section ]"></A>
|
|
<!--::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::-->
|
|
</body>
|
|
</html>
|
|
<!--endcut ========================================================= -->
|
|
|
|
|