ping.html

来自「perl教程」· HTML 代码 · 共 448 行 · 第 1/2 页

HTML
448
字号
<?xml version="1.0" ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<!-- saved from url=(0017)http://localhost/ -->
<script language="JavaScript" src="../../displayToc.js"></script>
<script language="JavaScript" src="../../tocParas.js"></script>
<script language="JavaScript" src="../../tocTab.js"></script>
<link rel="stylesheet" type="text/css" href="../../scineplex.css">
<title>Net::Ping - check a remote host for reachability</title>
<link rel="stylesheet" href="../../Active.css" type="text/css" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rev="made" href="mailto:" />
</head>

<body>

<script>writelinks('__top__',2);</script>
<h1><a>Net::Ping - check a remote host for reachability</a></h1>
<p><a name="__index__"></a></p>

<!-- INDEX BEGIN -->

<ul>

	<li><a href="#name">NAME</a></li>
	<li><a href="#synopsis">SYNOPSIS</a></li>
	<li><a href="#description">DESCRIPTION</a></li>
	<ul>

		<li><a href="#functions">Functions</a></li>
	</ul>

	<li><a href="#notes">NOTES</a></li>
	<li><a href="#install">INSTALL</a></li>
	<li><a href="#bugs">BUGS</a></li>
	<li><a href="#authors">AUTHORS</a></li>
	<li><a href="#copyright">COPYRIGHT</a></li>
</ul>
<!-- INDEX END -->

<hr />
<p>
</p>
<h1><a name="name">NAME</a></h1>
<p>Net::Ping - check a remote host for reachability</p>
<p>
</p>
<hr />
<h1><a name="synopsis">SYNOPSIS</a></h1>
<pre>
    <span class="keyword">use</span> <span class="variable">Net::Ping</span><span class="operator">;</span>
</pre>
<pre>
    <span class="variable">$p</span> <span class="operator">=</span> <span class="variable">Net::Ping</span><span class="operator">-&gt;</span><span class="variable">new</span><span class="operator">();</span>
    <span class="keyword">print</span> <span class="string">"$host is alive.\n"</span> <span class="keyword">if</span> <span class="variable">$p</span><span class="operator">-&gt;</span><span class="variable">ping</span><span class="operator">(</span><span class="variable">$host</span><span class="operator">);</span>
    <span class="variable">$p</span><span class="operator">-&gt;</span><span class="keyword">close</span><span class="operator">();</span>
</pre>
<pre>
    <span class="variable">$p</span> <span class="operator">=</span> <span class="variable">Net::Ping</span><span class="operator">-&gt;</span><span class="variable">new</span><span class="operator">(</span><span class="string">"icmp"</span><span class="operator">);</span>
    <span class="variable">$p</span><span class="operator">-&gt;</span><span class="keyword">bind</span><span class="operator">(</span><span class="variable">$my_addr</span><span class="operator">);</span> <span class="comment"># Specify source interface of pings</span>
    <span class="keyword">foreach</span> <span class="variable">$host</span> <span class="operator">(</span><span class="variable">@host_array</span><span class="operator">)</span>
    <span class="operator">{</span>
        <span class="keyword">print</span> <span class="string">"$host is "</span><span class="operator">;</span>
        <span class="keyword">print</span> <span class="string">"NOT "</span> <span class="keyword">unless</span> <span class="variable">$p</span><span class="operator">-&gt;</span><span class="variable">ping</span><span class="operator">(</span><span class="variable">$host</span><span class="operator">,</span> <span class="number">2</span><span class="operator">);</span>
        <span class="keyword">print</span> <span class="string">"reachable.\n"</span><span class="operator">;</span>
        <span class="keyword">sleep</span><span class="operator">(</span><span class="number">1</span><span class="operator">);</span>
    <span class="operator">}</span>
    <span class="variable">$p</span><span class="operator">-&gt;</span><span class="keyword">close</span><span class="operator">();</span>
</pre>
<pre>
    <span class="variable">$p</span> <span class="operator">=</span> <span class="variable">Net::Ping</span><span class="operator">-&gt;</span><span class="variable">new</span><span class="operator">(</span><span class="string">"tcp"</span><span class="operator">,</span> <span class="number">2</span><span class="operator">);</span>
    <span class="comment"># Try connecting to the www port instead of the echo port</span>
    <span class="variable">$p</span><span class="operator">-&gt;</span><span class="operator">{</span><span class="string">port_num</span><span class="operator">}</span> <span class="operator">=</span> <span class="keyword">getservbyname</span><span class="operator">(</span><span class="string">"http"</span><span class="operator">,</span> <span class="string">"tcp"</span><span class="operator">);</span>
    <span class="keyword">while</span> <span class="operator">(</span><span class="variable">$stop_time</span> <span class="operator">&gt;</span> <span class="keyword">time</span><span class="operator">())</span>
    <span class="operator">{</span>
        <span class="keyword">print</span> <span class="string">"$host not reachable "</span><span class="operator">,</span> <span class="keyword">scalar</span><span class="operator">(</span><span class="keyword">localtime</span><span class="operator">()),</span> <span class="string">"\n"</span>
            <span class="keyword">unless</span> <span class="variable">$p</span><span class="operator">-&gt;</span><span class="variable">ping</span><span class="operator">(</span><span class="variable">$host</span><span class="operator">);</span>
        <span class="keyword">sleep</span><span class="operator">(</span><span class="number">300</span><span class="operator">);</span>
    <span class="operator">}</span>
    <span class="keyword">undef</span><span class="operator">(</span><span class="variable">$p</span><span class="operator">);</span>
</pre>
<pre>
    <span class="comment"># Like tcp protocol, but with many hosts</span>
    <span class="variable">$p</span> <span class="operator">=</span> <span class="variable">Net::Ping</span><span class="operator">-&gt;</span><span class="variable">new</span><span class="operator">(</span><span class="string">"syn"</span><span class="operator">);</span>
    <span class="variable">$p</span><span class="operator">-&gt;</span><span class="operator">{</span><span class="string">port_num</span><span class="operator">}</span> <span class="operator">=</span> <span class="keyword">getservbyname</span><span class="operator">(</span><span class="string">"http"</span><span class="operator">,</span> <span class="string">"tcp"</span><span class="operator">);</span>
    <span class="keyword">foreach</span> <span class="variable">$host</span> <span class="operator">(</span><span class="variable">@host_array</span><span class="operator">)</span> <span class="operator">{</span>
      <span class="variable">$p</span><span class="operator">-&gt;</span><span class="variable">ping</span><span class="operator">(</span><span class="variable">$host</span><span class="operator">);</span>
    <span class="operator">}</span>
    <span class="keyword">while</span> <span class="operator">((</span><span class="variable">$host</span><span class="operator">,</span><span class="variable">$rtt</span><span class="operator">,</span><span class="variable">$ip</span><span class="operator">)</span> <span class="operator">=</span> <span class="variable">$p</span><span class="operator">-&gt;</span><span class="variable">ack</span><span class="operator">)</span> <span class="operator">{</span>
      <span class="keyword">print</span> <span class="string">"HOST: $host [$ip] ACKed in $rtt seconds.\n"</span><span class="operator">;</span>
    <span class="operator">}</span>
</pre>
<pre>
    <span class="comment"># High precision syntax (requires Time::HiRes)</span>
    <span class="variable">$p</span> <span class="operator">=</span> <span class="variable">Net::Ping</span><span class="operator">-&gt;</span><span class="variable">new</span><span class="operator">();</span>
    <span class="variable">$p</span><span class="operator">-&gt;</span><span class="variable">hires</span><span class="operator">();</span>
    <span class="operator">(</span><span class="variable">$ret</span><span class="operator">,</span> <span class="variable">$duration</span><span class="operator">,</span> <span class="variable">$ip</span><span class="operator">)</span> <span class="operator">=</span> <span class="variable">$p</span><span class="operator">-&gt;</span><span class="variable">ping</span><span class="operator">(</span><span class="variable">$host</span><span class="operator">,</span> <span class="number">5.5</span><span class="operator">);</span>
    <span class="keyword">printf</span><span class="operator">(</span><span class="string">"$host [ip: $ip] is alive (packet return time: %.2f ms)\n"</span><span class="operator">,</span> <span class="number">1000</span> <span class="operator">*</span> <span class="variable">$duration</span><span class="operator">)</span>
      <span class="keyword">if</span> <span class="variable">$ret</span><span class="operator">;</span>
    <span class="variable">$p</span><span class="operator">-&gt;</span><span class="keyword">close</span><span class="operator">();</span>
</pre>
<pre>
    <span class="comment"># For backward compatibility</span>
    <span class="keyword">print</span> <span class="string">"$host is alive.\n"</span> <span class="keyword">if</span> <span class="variable">pingecho</span><span class="operator">(</span><span class="variable">$host</span><span class="operator">);</span>
</pre>
<p>
</p>
<hr />
<h1><a name="description">DESCRIPTION</a></h1>
<p>This module contains methods to test the reachability of remote
hosts on a network.  A ping object is first created with optional
parameters, a variable number of hosts may be pinged multiple
times and then the connection is closed.</p>
<p>You may choose one of six different protocols to use for the
ping. The &quot;tcp&quot; protocol is the default. Note that a live remote host
may still fail to be pingable by one or more of these protocols. For
example, www.microsoft.com is generally alive but not &quot;icmp&quot; pingable.</p>
<p>With the &quot;tcp&quot; protocol the <a href="#item_ping"><code>ping()</code></a> method attempts to establish a
connection to the remote host's echo port.  If the connection is
successfully established, the remote host is considered reachable.  No
data is actually echoed.  This protocol does not require any special
privileges but has higher overhead than the &quot;udp&quot; and &quot;icmp&quot; protocols.</p>
<p>Specifying the &quot;udp&quot; protocol causes the <a href="#item_ping"><code>ping()</code></a> method to send a udp
packet to the remote host's echo port.  If the echoed packet is
received from the remote host and the received packet contains the
same data as the packet that was sent, the remote host is considered
reachable.  This protocol does not require any special privileges.
It should be borne in mind that, for a udp ping, a host
will be reported as unreachable if it is not running the
appropriate echo service.  For Unix-like systems see <em>inetd(8)</em>
for more information.</p>
<p>If the &quot;icmp&quot; protocol is specified, the <a href="#item_ping"><code>ping()</code></a> method sends an icmp
echo message to the remote host, which is what the UNIX ping program
does.  If the echoed message is received from the remote host and
the echoed information is correct, the remote host is considered
reachable.  Specifying the &quot;icmp&quot; protocol requires that the program
be run as root or that the program be setuid to root.</p>
<p>If the &quot;external&quot; protocol is specified, the <a href="#item_ping"><code>ping()</code></a> method attempts to
use the <code>Net::Ping::External</code> module to ping the remote host.
<code>Net::Ping::External</code> interfaces with your system's default <a href="#item_ping"><code>ping</code></a>
utility to perform the ping, and generally produces relatively
accurate results. If <code>Net::Ping::External</code> if not installed on your
system, specifying the &quot;external&quot; protocol will result in an error.</p>
<p>If the &quot;syn&quot; protocol is specified, the <a href="#item_ping"><code>ping()</code></a> method will only
send a TCP SYN packet to the remote host then immediately return.
If the syn packet was sent successfully, it will return a true value,
otherwise it will return false.  NOTE: Unlike the other protocols,
the return value does NOT determine if the remote host is alive or
not since the full TCP three-way handshake may not have completed
yet.  The remote host is only considered reachable if it receives
a TCP ACK within the timeout specifed.  To begin waiting for the
ACK packets, use the <a href="#item_ack"><code>ack()</code></a> method as explained below.  Use the
&quot;syn&quot; protocol instead the &quot;tcp&quot; protocol to determine reachability
of multiple destinations simultaneously by sending parallel TCP
SYN packets.  It will not block while testing each remote host.
demo/fping is provided in this distribution to demonstrate the
&quot;syn&quot; protocol as an example.
This protocol does not require any special privileges.</p>
<p>
</p>
<h2><a name="functions">Functions</a></h2>
<dl>
<dt><strong><a name="item_new">Net::Ping-&gt;new([$proto [, $def_timeout [, $bytes [, $device [, $tos ]]]]]);</a></strong>

<dd>
<p>Create a new ping object.  All of the parameters are optional.  $proto
specifies the protocol to use when doing a ping.  The current choices
are &quot;tcp&quot;, &quot;udp&quot;, &quot;icmp&quot;, &quot;stream&quot;, &quot;syn&quot;, or &quot;external&quot;.
The default is &quot;tcp&quot;.</p>
</dd>
<dd>
<p>If a default timeout ($def_timeout) in seconds is provided, it is used
when a timeout is not given to the <a href="#item_ping"><code>ping()</code></a> method (below).  The timeout
must be greater than 0 and the default, if not specified, is 5 seconds.</p>
</dd>
<dd>
<p>If the number of data bytes ($bytes) is given, that many data bytes
are included in the ping packet sent to the remote host. The number of
data bytes is ignored if the protocol is &quot;tcp&quot;.  The minimum (and
default) number of data bytes is 1 if the protocol is &quot;udp&quot; and 0
otherwise.  The maximum number of data bytes that can be specified is
1024.</p>
</dd>
<dd>
<p>If $device is given, this device is used to bind the source endpoint
before sending the ping packet.  I beleive this only works with
superuser privileges and with udp and icmp protocols at this time.</p>
</dd>
<dd>
<p>If $tos is given, this ToS is configured into the soscket.</p>
</dd>
</li>
<dt><strong><a name="item_ping">$p-&gt;ping($host [, $timeout]);</a></strong>

<dd>
<p>Ping the remote host and wait for a response.  $host can be either the
hostname or the IP number of the remote host.  The optional timeout
must be greater than 0 seconds and defaults to whatever was specified
when the ping object was created.  Returns a success flag.  If the
hostname cannot be found or there is a problem with the IP number, the
success flag returned will be undef.  Otherwise, the success flag will
be 1 if the host is reachable and 0 if it is not.  For most practical
purposes, undef and 0 and can be treated as the same case.  In array
context, the elapsed time as well as the string form of the ip the
host resolved to are also returned.  The elapsed time value will
be a float, as retuned by the Time::HiRes::time() function, if <a href="#item_hires"><code>hires()</code></a>
has been previously called, otherwise it is returned as an integer.</p>
</dd>
</li>
<dt><strong><a name="item_source_verify">$p-&gt;source_verify( { 0 | 1 } );</a></strong>

<dd>
<p>Allows source endpoint verification to be enabled or disabled.
This is useful for those remote destinations with multiples
interfaces where the response may not originate from the same
endpoint that the original destination endpoint was sent to.
This only affects udp and icmp protocol pings.</p>
</dd>
<dd>
<p>This is enabled by default.</p>
</dd>
</li>
<dt><strong><a name="item_service_check">$p-&gt;service_check( { 0 | 1 } );</a></strong>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?