剔除不能使用IP的小脚本

该脚本把ping不通的IP去除。
use warnings;
#use strict;
use Net::Ping;

#default conf
my $conf = "/usr/local/squid/etc/cidrlist.conf";

$version = 1.0;
if(@ARGV > 0)
{
    if(@ARGV == 1 and !($ARGV[0] cmp "-v"))
    {
        print "IP detect Version: ", $version, "\r\n";
        exit;
    }
    elsif(@ARGV ==1 and !($ARGV[0] cmp "-h"))
    {
        print "Use \"ip_detect -v\" for version number.", "\r\n";
        print "Use \"-h\" for help.", "\r\n";
        exit;
    }
    elsif(@ARGV ==2 and !($ARGV[0] cmp "-f"))
    {
        $conf = $ARGV[1];
    }
    else
    {
        print "error argument.", "\r\n";
        print "Use \"ip_detct -h\" for help.", "\r\n";
        exit;
    }
}
open FH, $conf or die "can't open file $conf $!" ;
my $conf_tmp = $conf.".tmp";
open NEWFH,">$conf_tmp" or die "can't open file $conf_tmp";

while(<FH>)
{
    if(/^\s*#/)
    {
        print NEWFH $_;
        next;   
  }

    if(/^\s*$/)
    {
        print NEWFH $_;
        next;
    }

    if(/\S*start|end\S*/)
    {
        print NEWFH $_;
        next;
    }

    chomp;

    my $host = $_;

    $p = Net::Ping->new("icmp");

    if(!$p->ping($host,2)){
        print "$host is not ";
        print "reachable.\n";
    }
    else
    {
        print "$host is ";
        print "reachable.\n";
        print NEWFH $host."\n";
    }
    $p->close();
}


rename($conf_tmp,$conf) || die "rename $conf_tmp to $conf errori $!\n";

close FH;
close NEWFH;

你可能感兴趣的:(File,脚本)