免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 6847 | 回复: 4
打印 上一主题 下一主题

Perl Net::Ping 求解 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-11-01 15:32 |只看该作者 |倒序浏览
本帖最后由 xuezhilei40308 于 2011-11-01 15:33 编辑

问题:
脚本中使用perl Net::Ping 模块来监控服务器是否能ping的通。脚本在测试环境中没有问题,但是放到客户环境中后,出现了一个问题:服务器有时候ping自己ping不通,返回down的状态;有时候又能ping通自己,返回up 的状态。当返回down的状态的时候,产生告警,此时客户在终端上用command "ping  $servername" 确是通的,没有丢包的现象。我仔细查看了代码,没有发现任何逻辑上的问题。

求解:
        1. Perl Net::Ping 模块存在bug? 环境使用的perl版本是5.8.8
        2. 代码有bug?
        3. 如何追踪这个问题?网络的问题不太懂。请高手帮忙。

代码:

sub ping_a_host{
        my ($subname) = (caller(0))[3];
        my $host=shift;
        chomp($host);
        my $a=Net::Ping->new("icmp",3);
        logger($subname,$DEBUG,"ping host $host");
                        if(!$a->ping($host)){
                                &send_down_msg($host);
                                logger($subname,$DEBUG,"$host is down");
                                        $a->close();
                        }elsif($down_nodes =~/(^|\:)\Q$host\E($|\:)/g) {
                                logger($subname,$DEBUG,"$host is alive again.");
                                send_up_msg($host);
                        }      
        $a->close();
}

论坛徽章:
0
2 [报告]
发表于 2011-11-01 16:02 |只看该作者
回复 1# xuezhilei40308


    那个机器是windows还是linux/unix
    你是以那个用户来运行这个代码的

   https://metacpan.org/module/Net:\"\"ing

DESCRIPTION

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.

You may choose one of six different protocols to use for the ping. The "tcp" 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 "icmp" pingable.

With the "tcp" protocol the ping() 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 "udp" and "icmp" protocols.

Specifying the "udp" protocol causes the ping() 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 inetd( for more information.

If the "icmp" protocol is specified, the ping() 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 "icmp" protocol requires that the program be run as root or that the program be setuid to root.

If the "external" protocol is specified, the ping() method attempts to use the Net:ing::External module to ping the remote host. Net:ing::External interfaces with your system's default ping utility to perform the ping, and generally produces relatively accurate results. If Net:ing::External if not installed on your system, specifying the "external" protocol will result in an error.

If the "syn" protocol is specified, the ping() 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 specified. To begin waiting for the ACK packets, use the ack() method as explained below. Use the "syn" protocol instead the "tcp" 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 "syn" protocol as an example. This protocol does not require any special privileges.

论坛徽章:
0
3 [报告]
发表于 2011-11-01 16:05 |只看该作者
谢谢回复喔~

服务器是HPUX。是root用户执行的脚本。

论坛徽章:
0
4 [报告]
发表于 2011-11-02 01:03 |只看该作者
试试
  1. Net::Ping::External
复制代码

论坛徽章:
0
5 [报告]
发表于 2014-06-09 00:21 |只看该作者
我今天用net::ping也遇到这个问题, Linux ping命令明明是通的, 但是net::ping返回为不通, 后来只好用if (`ping -c 1 $host |grep ttl` ne "") {}
代替。 想知道为啥net::ping 会不准确。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP