2

如何查找 IP 地址是否可 ping?另外,如何使用 perl 脚本找到可 ping 的 IP 是静态的还是动态的?

4

3 回答 3

3

看看Net::Ping模块;

#!/usr/bin/env perl
#
use strict;
use warnings;

use Net::Ping;

my $ip_address = shift || die "Need an IP address (or hostname).\n";

my $p = Net::Ping->new();
if ( $p->ping($ip_address) ) {
    print "Success!\n";
}
else {
    print "Fail!\n";
}

找出一个 IP 地址是动态的还是静态的需要更多的工作。看看这个这个帖子。

于 2012-11-19T13:08:15.817 回答
2

这样的事情可能有助于检查主机是否响应 ICMP:

#!/usr/bin/perl

use strict;
use warnings;
use Net::Ping;

my (@alive_hosts, @dead_hosts);

my $ping = Net::Ping->new;

while (my $host = <DATA>) {
        next if $host =~ /^\s*$/;
        chomp $host;
        if ($ping->ping($host)) {
                push @alive_hosts, $host;
        } else {
                push @dead_hosts, $host;
        }
}

if (@alive_hosts) {
        print "Alive hosts\n" . "-" x 10 . "\n";
        print join ("\n", sort @alive_hosts) . "\n\n"
}

if (@dead_hosts) {
        print "Dead hosts\n" . "-" x 10 . "\n";
        print join ("\n", sort @dead_hosts) . "\n\n";
}

__DATA__
server1
server2
server3

The result would be something like:

Alive hosts
----------
server1
server2

Dead hosts
----------
server3

I'm not sure about your second requirement.

于 2012-11-19T13:16:59.710 回答
1

如何查找 IP 地址是否可 ping?

[mpenning@tsunami ~]$ perl -e '$retval=system("ping -c 2 172.16.1.1");if ($retval==0) {print "It pings";} else { print "ping failed"; }'
PING 172.16.1.1 (172.16.1.1) 56(84) bytes of data.
64 bytes from 172.16.1.1: icmp_req=1 ttl=255 time=0.384 ms
64 bytes from 172.16.1.1: icmp_req=2 ttl=255 time=0.416 ms

--- 172.16.1.1 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 999ms
rtt min/avg/max/mdev = 0.384/0.400/0.416/0.016 ms
It pings[mpenning@tsunami ~]$

以更友好的形式...

$retval=system("ping -c 2 172.16.1.1");
if ($retval==0) {
    print "It pings\n";
} else {
    print "ping failed\n";
}

另外,如何使用 perl 脚本找到可 ping 的 IP 是静态的还是动态的?

如果您可以直接访问 DHCP 服务器,则只能执行此操作,或者您可以嗅探子网并查找 DHCP 数据包。如果没有更多信息,我们还不能回答这个问题。

于 2012-11-19T11:36:47.103 回答