my @matches = grep {defined $_} map { /^($subnet\.\d+)/ and $1; } @lines; #### #!/usr/bin/perl -w use Getopt::Std; use Net::Netmask; my $subnet; my $ARP = '/tmp/arp.bak'; getopt('sm'); if ($opt_s ne '') { $subnet = $opt_s; } else { die "Please supply a subnet to scan."; } my $block = new Net::Netmask($subnet); my @range = $block->enumerate(); #We need to get our temp file system `tail -250000 /tmp/arp > $ARP`; my @lines = ; chomp(@lines); #Get the IPs that are preset with first 3 octets matching my @matches = grep {defined $_} map { /^($subnet\.\d+)/ and $1; } @lines; #Get the difference of the block and the IP's found my @intersection = my @difference = (); my %count = (); foreach $element (@matches, @range) { $count{$element}++ } foreach $element (keys %count) { push @{ $count{$element} > 1 ? \@intersection : \@difference }, $element; } #make unique and then sort undef %pre; @pre{@difference} = (); @clean = keys %pre; my @sorted = sort { pack('C4' => $a =~ /(\d+)\.(\d+)\.(\d+)\.(\d+)/) cmp pack('C4' => $b =~ /(\d+)\.(\d+)\.(\d+)\.(\d+)/) } @clean; print join("\n",@sorted); __DATA__ 192.168.87.134 0x00804F429376 Vl2n22 192.168.87.135 0x00804F429466 Vl2n22 192.168.87.136 0x00804F4288E1 Vl2n22 192.168.87.138 0x00804F425776 Vl2n22 192.168.87.139 0x00804F461812 Vl2n22 192.168.87.144 0x0060B0280D05 Vl2n22 192.168.87.146 0x0001E62B36E7 Vl2n22 192.168.87.147 0x0060B030889D Vl2n22 192.168.87.148 0x0060B0521032 Vl2n22 192.168.87.149 0x00804F45F98E Vl2n22 192.168.87.150 0x00804F559E8D Vl2n22 192.168.87.151 0x00804F4627D8 Vl2n22 192.168.87.153 0x00804F58469D Vl2n22 192.168.87.156 0x0060B032E627 Vl2n22 192.168.87.157 0x00804F4627B2 Vl2n22 192.168.87.159 0x00804F462788 Vl2n22 192.168.89.163 0x00008086DEE1 Vl2n22 192.168.89.164 0x00804F41D3F7 Vl2n22 192.168.89.165 0x00804F4294F2 Vl2n22 192.168.89.167 0x00804F1E81F9 Vl2n22 192.168.87.168 0x00902784B532 Vl2n22 192.168.87.169 0x0060B012F54E Vl2n22 192.168.89.171 0x0040883FB2D1 Vl2n22 192.168.87.172 0x0001E63D35BE Vl2n22 192.168.89.173 0x0060B0F270BB Vl2n22 192.168.87.250 0x00405801279B Vl2n22 192.168.87.251 0x0040580008D6 Vl2n22