Linux 无法使用Net::Ping在Perl中Ping外部主机
在Perl(5.16.3)中,我尝试使用Net::Ping测试远程主机是否可用。我可以ping“内部”主机,我知道这些主机在我公司的局域网内是在线的,但我不能ping“外部”主机。具体来说,尝试ping“www.google.com”失败了 我的代码:Linux 无法使用Net::Ping在Perl中Ping外部主机,linux,perl,Linux,Perl,在Perl(5.16.3)中,我尝试使用Net::Ping测试远程主机是否可用。我可以ping“内部”主机,我知道这些主机在我公司的局域网内是在线的,但我不能ping“外部”主机。具体来说,尝试ping“www.google.com”失败了 我的代码: #!/usr/bin/perl use strict; use warnings; use Net::Ping; my $hostname1 = 'prophet'; #internal my $hostname2 = 'www.google
#!/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程序中):
然后,在我运行程序之前,su-root将起作用
[oracle@prophet:bin]$ su root
Password:
[root@prophet:bin]# ./ping_test
Response1: 1
Response2: 1
[root@prophet:bin]#
。。。但是,我希望能够使用Net::Ping(w/icmp数据包)而不必su-root。这实际上是我需要编写的自动化程序的一个要求。对于我来说,我可以作为普通用户运行ping(CentOS)实用程序并获得预期结果似乎有点疯狂,但尝试作为普通用户使用Net::ping是不可能的
有什么想法吗
- G
-rwsr-xr-x 1 root root 44168 May 7 2014 /bin/ping
不管你喜欢与否,使用ICMP本质上需要根权限。你不能像普通用户那样做
如果要检查连接,请考虑制作TCP连接。(或者,见鬼,一个完整的HTTP请求。)
您可以作为非root用户运行ping
的原因是它使用setuid root,或者在某些系统上设置了CAP_NET_RAW功能。可能的解决方法是:用C编写一个包装器,在包装器上执行脚本和setuid root,如中所述。@ThisSuitesBlacknot,太复杂了。只需使用-c
选项执行ping
实用程序并检查退出代码。
-rwsr-xr-x 1 root root 44168 May 7 2014 /bin/ping