jcht_mck <[EMAIL PROTECTED]> asked:
> I wanted to print if it matches my range of IP's.

May I suggest a different approach:

#!/usr/bin/perl -w

use strict;
use Socket; # we need Socket.pm for inet_aton()

#
# number of mask bits => inverted Bit mask
#
# converts the xx number of mask bits (in ip.ip.ip.ip/xx) to an
# inverted bit mask, i.e. 24 becomes 0x000000FF
#

sub make_mask {

  if( $_[0] < 0 || $_[0] > 32 ){
    die "Illegal mask /$_[0]";
  } else {
    return( 2 ** ( 32 - $_[0] ) - 1);
  }
}

#
# ip.ip.ip.ip => 0x????????
#
# convert a dottect decimal IP address to an unsigned long number
# in host byte order.
#

sub ip_to_number {

  if( defined ( my $nl = inet_aton( $_[0] ) ) ){
    # convert network to host byte order
    return( unpack( 'N', $nl ) );
  } else {
    die "Invalid IP address '$_[0]'!\n";
  }

}

# assume our network is 192.168.1.0/24
# i.e. 192.168.1.0 netmask 255.255.255.0
# i.e. 192.168.1.0 - 192.168.1.255
my $low_addr = ip_to_number( "192.168.1.0" );
my $high_addr = $low_addr + make_mask("24");

while( <DATA> ){
  chomp( $_ );
  my $addr = ip_to_number( $_ );
  print "$_ is in my net" if $low_addr <= $addr && $addr <= $high_addr;
}

__DATA__
127.0.0.1
192.168.1.17
213.144.21.251


HTH,
Thomas

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to