• 要找福利?记不住网址?搜不到?如何与我们保持联系?试试 CTRL+D 把零点博客添加进您的浏览器收藏夹,一键直达永不失联!

网络通讯Linux ping命令

linux系统 涛涛 113次浏览 0个评论

Linux ping命令用于检测主机。

执行ping指令会使用ICMP传输协议,发出要求回应的信息,若远端主机的网络功能没有问题,就会回应该信息,因而得知该主机运作正常。

语法

<span class="pln">ping </span><span class="pun">[-</span><span class="pln">dfnqrRv</span><span class="pun">][-</span><span class="pln">c</span><span class="pun"><完成次数>][-</span><span class="pln">i</span><span class="pun"><间隔秒数>][-</span><span class="pln">I</span><span class="pun"><网络界面>][-</span><span class="pln">l</span><span class="pun"><前置载入>][-</span><span class="pln">p</span><span class="pun"><范本样式>][-</span><span class="pln">s</span><span class="pun"><数据包大小>][-</span><span class="pln">t</span><span class="pun"><存活数值>][主机名称或</span><span class="pln">IP</span><span class="pun">地址]</span>

参数说明

  • -d 使用Socket的SO_DEBUG功能。
  • -c<完成次数> 设置完成要求回应的次数。
  • -f 极限检测。
  • -i<间隔秒数> 指定收发信息的间隔时间。
  • -I<网络界面> 使用指定的网络接口送出数据包。
  • -l<前置载入> 设置在送出要求信息之前,先行发出的数据包。
  • -n 只输出数值。
  • -p<范本样式> 设置填满数据包的范本样式。
  • -q 不显示指令执行过程,开头和结尾的相关信息除外。
  • -r 忽略普通的Routing Table,直接将数据包送到远端主机上。
  • -R 记录路由过程。
  • -s<数据包大小> 设置数据包的大小。
  • -t<存活数值> 设置存活数值TTL的大小。
  • -v 详细显示指令的执行过程。

实例

检测是否与主机连通

<span class="com"># ping www.w3cschool.cc //ping主机</span><span class="pln">
PING aries</span><span class="pun">.</span><span class="pln">m</span><span class="pun">.</span><span class="pln">alikunlun</span><span class="pun">.</span><span class="pln">com </span><span class="pun">(</span><span class="lit">114.80</span><span class="pun">.</span><span class="lit">174.110</span><span class="pun">)</span> <span class="lit">56</span><span class="pun">(</span><span class="lit">84</span><span class="pun">)</span><span class="pln"> bytes of data</span><span class="pun">.</span>
<span class="lit">64</span><span class="pln"> bytes </span><span class="kwd">from</span> <span class="lit">114.80</span><span class="pun">.</span><span class="lit">174.110</span><span class="pun">:</span><span class="pln"> icmp_seq</span><span class="pun">=</span><span class="lit">1</span><span class="pln"> ttl</span><span class="pun">=</span><span class="lit">64</span><span class="pln"> time</span><span class="pun">=</span><span class="lit">0.025</span><span class="pln"> ms
</span><span class="lit">64</span><span class="pln"> bytes </span><span class="kwd">from</span> <span class="lit">114.80</span><span class="pun">.</span><span class="lit">174.110</span><span class="pun">:</span><span class="pln"> icmp_seq</span><span class="pun">=</span><span class="lit">2</span><span class="pln"> ttl</span><span class="pun">=</span><span class="lit">64</span><span class="pln"> time</span><span class="pun">=</span><span class="lit">0.036</span><span class="pln"> ms
</span><span class="lit">64</span><span class="pln"> bytes </span><span class="kwd">from</span> <span class="lit">114.80</span><span class="pun">.</span><span class="lit">174.110</span><span class="pun">:</span><span class="pln"> icmp_seq</span><span class="pun">=</span><span class="lit">3</span><span class="pln"> ttl</span><span class="pun">=</span><span class="lit">64</span><span class="pln"> time</span><span class="pun">=</span><span class="lit">0.034</span><span class="pln"> ms
</span><span class="lit">64</span><span class="pln"> bytes </span><span class="kwd">from</span> <span class="lit">114.80</span><span class="pun">.</span><span class="lit">174.110</span><span class="pun">:</span><span class="pln"> icmp_seq</span><span class="pun">=</span><span class="lit">4</span><span class="pln"> ttl</span><span class="pun">=</span><span class="lit">64</span><span class="pln"> time</span><span class="pun">=</span><span class="lit">0.034</span><span class="pln"> ms
</span><span class="lit">64</span><span class="pln"> bytes </span><span class="kwd">from</span> <span class="lit">114.80</span><span class="pun">.</span><span class="lit">174.110</span><span class="pun">:</span><span class="pln"> icmp_seq</span><span class="pun">=</span><span class="lit">5</span><span class="pln"> ttl</span><span class="pun">=</span><span class="lit">64</span><span class="pln"> time</span><span class="pun">=</span><span class="lit">0.028</span><span class="pln"> ms
</span><span class="lit">64</span><span class="pln"> bytes </span><span class="kwd">from</span> <span class="lit">114.80</span><span class="pun">.</span><span class="lit">174.110</span><span class="pun">:</span><span class="pln"> icmp_seq</span><span class="pun">=</span><span class="lit">6</span><span class="pln"> ttl</span><span class="pun">=</span><span class="lit">64</span><span class="pln"> time</span><span class="pun">=</span><span class="lit">0.028</span><span class="pln"> ms
</span><span class="lit">64</span><span class="pln"> bytes </span><span class="kwd">from</span> <span class="lit">114.80</span><span class="pun">.</span><span class="lit">174.110</span><span class="pun">:</span><span class="pln"> icmp_seq</span><span class="pun">=</span><span class="lit">7</span><span class="pln"> ttl</span><span class="pun">=</span><span class="lit">64</span><span class="pln"> time</span><span class="pun">=</span><span class="lit">0.034</span><span class="pln"> ms
</span><span class="lit">64</span><span class="pln"> bytes </span><span class="kwd">from</span> <span class="lit">114.80</span><span class="pun">.</span><span class="lit">174.110</span><span class="pun">:</span><span class="pln"> icmp_seq</span><span class="pun">=</span><span class="lit">8</span><span class="pln"> ttl</span><span class="pun">=</span><span class="lit">64</span><span class="pln"> time</span><span class="pun">=</span><span class="lit">0.034</span><span class="pln"> ms
</span><span class="lit">64</span><span class="pln"> bytes </span><span class="kwd">from</span> <span class="lit">114.80</span><span class="pun">.</span><span class="lit">174.110</span><span class="pun">:</span><span class="pln"> icmp_seq</span><span class="pun">=</span><span class="lit">9</span><span class="pln"> ttl</span><span class="pun">=</span><span class="lit">64</span><span class="pln"> time</span><span class="pun">=</span><span class="lit">0.036</span><span class="pln"> ms
</span><span class="lit">64</span><span class="pln"> bytes </span><span class="kwd">from</span> <span class="lit">114.80</span><span class="pun">.</span><span class="lit">174.110</span><span class="pun">:</span><span class="pln"> icmp_seq</span><span class="pun">=</span><span class="lit">10</span><span class="pln"> ttl</span><span class="pun">=</span><span class="lit">64</span><span class="pln"> time</span><span class="pun">=</span><span class="lit">0.041</span><span class="pln"> ms

</span><span class="pun">---</span><span class="pln"> aries</span><span class="pun">.</span><span class="pln">m</span><span class="pun">.</span><span class="pln">alikunlun</span><span class="pun">.</span><span class="pln">com ping statistics </span><span class="pun">---</span>
<span class="lit">10</span><span class="pln"> packets transmitted</span><span class="pun">,</span> <span class="lit">30</span><span class="pln"> received</span><span class="pun">,</span> <span class="lit">0</span><span class="pun">%</span><span class="pln"> packet loss</span><span class="pun">,</span><span class="pln"> time </span><span class="lit">29246ms</span><span class="pln">
rtt min</span><span class="pun">/</span><span class="pln">avg</span><span class="pun">/</span><span class="pln">max</span><span class="pun">/</span><span class="pln">mdev </span><span class="pun">=</span> <span class="lit">0.021</span><span class="pun">/</span><span class="lit">0.035</span><span class="pun">/</span><span class="lit">0.078</span><span class="pun">/</span><span class="lit">0.011</span><span class="pln"> ms

</span><span class="com">//需要手动终止Ctrl+C</span>

指定接收包的次数

<span class="com"># ping -c 2 www.w3cschool.cc</span><span class="pln">
PING aries</span><span class="pun">.</span><span class="pln">m</span><span class="pun">.</span><span class="pln">alikunlun</span><span class="pun">.</span><span class="pln">com </span><span class="pun">(</span><span class="lit">114.80</span><span class="pun">.</span><span class="lit">174.120</span><span class="pun">)</span> <span class="lit">56</span><span class="pun">(</span><span class="lit">84</span><span class="pun">)</span><span class="pln"> bytes of data</span><span class="pun">.</span>
<span class="lit">64</span><span class="pln"> bytes </span><span class="kwd">from</span> <span class="lit">114.80</span><span class="pun">.</span><span class="lit">174.120</span><span class="pun">:</span><span class="pln"> icmp_seq</span><span class="pun">=</span><span class="lit">1</span><span class="pln"> ttl</span><span class="pun">=</span><span class="lit">54</span><span class="pln"> time</span><span class="pun">=</span><span class="lit">6.18</span><span class="pln"> ms
</span><span class="lit">64</span><span class="pln"> bytes </span><span class="kwd">from</span> <span class="lit">114.80</span><span class="pun">.</span><span class="lit">174.120</span><span class="pun">:</span><span class="pln"> icmp_seq</span><span class="pun">=</span><span class="lit">2</span><span class="pln"> ttl</span><span class="pun">=</span><span class="lit">54</span><span class="pln"> time</span><span class="pun">=</span><span class="lit">15.4</span><span class="pln"> ms

</span><span class="pun">---</span><span class="pln"> aries</span><span class="pun">.</span><span class="pln">m</span><span class="pun">.</span><span class="pln">alikunlun</span><span class="pun">.</span><span class="pln">com ping statistics </span><span class="pun">---</span>
<span class="lit">2</span><span class="pln"> packets transmitted</span><span class="pun">,</span> <span class="lit">2</span><span class="pln"> received</span><span class="pun">,</span> <span class="lit">0</span><span class="pun">%</span><span class="pln"> packet loss</span><span class="pun">,</span><span class="pln"> time </span><span class="lit">1016ms</span><span class="pln">
rtt min</span><span class="pun">/</span><span class="pln">avg</span><span class="pun">/</span><span class="pln">max</span><span class="pun">/</span><span class="pln">mdev </span><span class="pun">=</span> <span class="lit">6.185</span><span class="pun">/</span><span class="lit">10.824</span><span class="pun">/</span><span class="lit">15.464</span><span class="pun">/</span><span class="lit">4.640</span><span class="pln"> ms

</span><span class="com">//收到两次包后,自动退出</span>

多参数使用

<span class="com"># ping -i 3 -s 1024 -t 255 g.cn //ping主机</span><span class="pln">
PING g</span><span class="pun">.</span><span class="pln">cn </span><span class="pun">(</span><span class="lit">203.208</span><span class="pun">.</span><span class="lit">37.104</span><span class="pun">)</span> <span class="lit">1024</span><span class="pun">(</span><span class="lit">1052</span><span class="pun">)</span><span class="pln"> bytes of data</span><span class="pun">.</span>
<span class="lit">1032</span><span class="pln"> bytes </span><span class="kwd">from</span><span class="pln"> bg</span><span class="pun">-</span><span class="kwd">in</span><span class="pun">-</span><span class="pln">f104</span><span class="pun">.</span><span class="lit">1e100</span><span class="pun">.</span><span class="pln">net </span><span class="pun">(</span><span class="lit">203.208</span><span class="pun">.</span><span class="lit">37.104</span><span class="pun">):</span><span class="pln"> icmp_seq</span><span class="pun">=</span><span class="lit">0</span><span class="pln"> ttl</span><span class="pun">=</span><span class="lit">243</span><span class="pln"> time</span><span class="pun">=</span><span class="lit">62.5</span><span class="pln"> ms
</span><span class="lit">1032</span><span class="pln"> bytes </span><span class="kwd">from</span><span class="pln"> bg</span><span class="pun">-</span><span class="kwd">in</span><span class="pun">-</span><span class="pln">f104</span><span class="pun">.</span><span class="lit">1e100</span><span class="pun">.</span><span class="pln">net </span><span class="pun">(</span><span class="lit">203.208</span><span class="pun">.</span><span class="lit">37.104</span><span class="pun">):</span><span class="pln"> icmp_seq</span><span class="pun">=</span><span class="lit">1</span><span class="pln"> ttl</span><span class="pun">=</span><span class="lit">243</span><span class="pln"> time</span><span class="pun">=</span><span class="lit">63.9</span><span class="pln"> ms
</span><span class="lit">1032</span><span class="pln"> bytes </span><span class="kwd">from</span><span class="pln"> bg</span><span class="pun">-</span><span class="kwd">in</span><span class="pun">-</span><span class="pln">f104</span><span class="pun">.</span><span class="lit">1e100</span><span class="pun">.</span><span class="pln">net </span><span class="pun">(</span><span class="lit">203.208</span><span class="pun">.</span><span class="lit">37.104</span><span class="pun">):</span><span class="pln"> icmp_seq</span><span class="pun">=</span><span class="lit">2</span><span class="pln"> ttl</span><span class="pun">=</span><span class="lit">243</span><span class="pln"> time</span><span class="pun">=</span><span class="lit">61.9</span><span class="pln"> ms

</span><span class="pun">---</span><span class="pln"> g</span><span class="pun">.</span><span class="pln">cn ping statistics </span><span class="pun">---</span>
<span class="lit">3</span><span class="pln"> packets transmitted</span><span class="pun">,</span> <span class="lit">3</span><span class="pln"> received</span><span class="pun">,</span> <span class="lit">0</span><span class="pun">%</span><span class="pln"> packet loss</span><span class="pun">,</span><span class="pln"> time </span><span class="lit">6001ms</span><span class="pln">
rtt min</span><span class="pun">/</span><span class="pln">avg</span><span class="pun">/</span><span class="pln">max</span><span class="pun">/</span><span class="pln">mdev </span><span class="pun">=</span> <span class="lit">61.959</span><span class="pun">/</span><span class="lit">62.843</span><span class="pun">/</span><span class="lit">63.984</span><span class="pun">/</span><span class="lit">0.894</span><span class="pln"> ms</span><span class="pun">,</span><span class="pln"> pipe </span><span class="lit">2</span>
<span class="pun">[</span><span class="pln">root@linux </span><span class="pun">~]#</span> 

<span class="com">//-i 3 发送周期为 3秒 -s 设置发送包的大小 -t 设置TTL值为 255</span>

欢迎转载本文:网络通讯Linux ping命令
喜欢 (0)
[ld]
分享 (0)
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址