【发布时间】:2015-02-27 20:05:03
【问题描述】:
在 Perl (5.16.3) 中,我尝试使用 Net::Ping 来测试远程主机是否可用。我可以 ping 我知道在我公司的 LAN 中在线的“内部”主机,但我无法 ping “外部”主机。具体来说,尝试 ping 'www.google.com' 失败。
我的代码:
#!/usr/bin/perl
use strict;
use warnings;
use Net::Ping;
my $hostname1 = 'prophet'; #internal
my $hostname2 = 'www.google.com'; #external
my $p;
my $rv1;
my $rv2;
$p = Net::Ping->new();
$rv1 = $p->ping($hostname1);
$rv2 = $p->ping($hostname2);
print "Response1: $rv1\n";
print "Response2: $rv2\n";
产生这个结果:
[oracle@prophet:bin]$ ./ping_test
Response1: 1
Response2: 0
[oracle@prophet:bin]$
即使使用 (CentOS) ping 实用程序确实显示“www.google.com”可用:
[oracle@prophet:bin]$ which ping; ping www.google.com
/usr/bin/ping
PING www.google.com (64.233.177.105) 56(84) bytes of data.
64 bytes from 64.233.177.105: icmp_seq=1 ttl=46 time=15.6 ms
64 bytes from 64.233.177.105: icmp_seq=2 ttl=46 time=15.6 ms
64 bytes from 64.233.177.105: icmp_seq=3 ttl=46 time=15.7 ms
64 bytes from 64.233.177.105: icmp_seq=4 ttl=46 time=16.5 ms
^C
--- www.google.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3005ms
rtt min/avg/max/mdev = 15.614/15.884/16.547/0.394 ms
[oracle@prophet:bin]$
我意识到如果我这样做(在我的 Perl 程序中):
$p = Net::Ping->new('icmp');
然后在我运行程序之前su - root,它会工作......
[oracle@prophet:bin]$ su root
Password:
[root@prophet:bin]# ./ping_test
Response1: 1
Response2: 1
[root@prophet:bin]#
...但是,我希望能够使用 Net::Ping(带 icmp 数据包)而无需不必su - root。这实际上是我需要编写的自动化程序的要求。我可以以普通用户身份运行 ping (CentOS) 实用程序并获得预期的结果,这对我来说似乎有点疯狂,但尝试以普通用户身份使用 Net::Ping 是不行的-去。
有什么想法吗?
- G
【问题讨论】:
-
您可以以非 root 用户身份运行
ping的原因是它使用 setuid root 或在某些系统上设置了 CAP_NET_RAW 功能。