Reputation: 663
I would like to remove all addresses that start with '192.18' from results. I keep either removing all addresses or none...
Here is the code.
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);
Upvotes: 0
Views: 65
Reputation: 1570
Have you considered using the contains() method from NetAddr::IP?
#!/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";
}
Which gives the following output:
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
Upvotes: 1