从$ result中删除特定的IP范围

时间:2014-06-02 12:47:16

标签: regex perl

我想删除以' 192.18'开头的所有地址。从结果。我继续删除所有地址或没有...

这是代码。

sub get_oids{
    my($starting_oid , $new_oid , $unique_oid , $result , $crap);
    my($ip , $name , $port , $type);
    $starting_oid = $_[0];
    $new_oid = $starting_oid ;

    while(Net::SNMP::oid_context_match($starting_oid,$new_oid)){
        $result = $session->get_next_request(($new_oid));
        return  unless (defined $result);

        ($new_oid , $crap) = %$result;
        if (Net::SNMP::oid_context_match($starting_oid,$new_oid)){
            $unique_oid = $new_oid;
            $unique_oid =~ s/$starting_oid//g;
            $ip = (Convert_IP(Get_SNMP_Info("$oid_root".".4"."$unique_
+oid")));
            $name = (Get_SNMP_Info("$oid_root".".6"."$unique_oid"));
            $port = (Get_SNMP_Info("$oid_root".".7"."$unique_oid"));
            $type = (Get_SNMP_Info("$oid_root".".8"."$unique_oid"));
            @todo=(@todo,$ip);
            write;
            get_oids($new_oid);

1 个答案:

答案 0 :(得分:1)

您是否考虑过使用NetAddr::IP中的contains()方法?

#!/usr/bin/env perl

use strict;
use warnings;
use feature 'say';

use NetAddr::IP;

my @ips = qw(192.168.0.1 192.18.0.1 192.18.22.44 255.255.255.0);

my $Range = NetAddr::IP->new('192.18.0.0/16');

for my $ip (@ips) {
    my $IP = NetAddr::IP->new($ip);
    my $contains = $Range->contains($IP) ? "yes" : "no";
    say "$ip: $contains";
}

其中给出了以下输出:

alex@yuzu:~$ ./net_addr_ip.pl
192.168.0.1: no
192.18.0.1: yes
192.18.22.244: yes
255.255.255.0: no