【发布时间】:2020-04-11 16:51:55
【问题描述】:
我在 Linux 上使用 Perl 5,版本 30。我想检查服务器是否处于活动状态,并且我只对 ping 调用返回 true 或 false 感兴趣。这是我的(非工作)代码:
#!/usr/bin/perl
use strict;
use warnings;
use Net::Ping;
my $pinger = Net::Ping->new();
if ($pinger->ping('google.com')) {
print 'alive\n';
} else {
print 'dead\n';
}
代码应该可以工作(我认为)。但是每次都会对每个服务器都失败(返回“dead”)。如果我以 sudo 执行它也会失败:sudo perl pingcheck.pl。 (编辑:我不能在实践中使用 sudo。我尝试它只是为了排除故障。)
我确实安装了Net::Ping:
$ cpan -l | grep Net::Ping
Net::Ping 2.71
没有来自 Perl 的错误消息。
如果我在 bash 中做同样的事情,ping 会按预期工作:
$ ping -c 3 google.com
PING google.com (64.233.178.100) 56(84) bytes of data.
64 bytes from ol-in-f100.1e100.net (64.233.178.100): icmp_seq=1 ttl=43 time=49.8 ms
64 bytes from ol-in-f100.1e100.net (64.233.178.100): icmp_seq=2 ttl=43 time=49.8 ms
64 bytes from ol-in-f100.1e100.net (64.233.178.100): icmp_seq=3 ttl=43 time=50.0 ms
--- google.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2004ms
rtt min/avg/max/mdev = 49.754/49.846/50.011/0.116 ms
【问题讨论】:
标签: perl tcp connection port ping