Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Checking IP's with an IP/mask

by jwest (Friar)
on Apr 24, 2001 at 00:21 UTC ( [id://74858]=note: print w/replies, xml ) Need Help??


in reply to Checking IP's with an IP/mask

Unfortunately, this is a terse answer, as I haven't done this myself before, but Net::Netmask should do the trick for you, I'd think.

use Net::Netmask; $block=new Net::Netmask("10.0.2.0/24"); if ($block->match("192.168.1.2")) { ... }
Update: added code example

-><- -><- -><- -><- -><-
All things are Perfect
    To every last Flaw
    And bound in accord
         With Eris's Law
 - HBT; The Book of Advice, 1:7

Replies are listed 'Best First'.
Re: Re: Checking IP's with an IP/mask
by idnopheq (Chaplain) on Apr 24, 2001 at 14:00 UTC
    OUTSTANDING!

    #!/usr/bin/perl -wT #-*-perl-*- use strict; use Net::Netmask; my $block=new Net::Netmask( $ARGV[0] ); if ($block->match( $ARGV[1] ) ) { print $ARGV[1] , " exists in " , $ARGV[0] , "\n"; } else { print "CIDR = " , $block->desc() , "\n"; # a.b.c. +d/bits print "IP Address = " , $block->base() , "\n"; print "Netmask = " , $block->mask() , "\n"; print "Hostmask = " , $block->hostmask() , "\n"; print "Mask bits = " , $block->bits() , "\n"; print "Subnet size = " , $block->size() , "\n"; print "Max netmask = " , $block->maxblock() , "\n"; print "Broadcast = " , $block->broadcast() , "\n"; print "Next netmask = " , $block->next() , "\n"; print "First host = " , $block->nth(1) , "\n"; }

    I wish I had found this module a few years ago! My binary math is quite slow.

    THX!
    --
    idnopheq
    Apply yourself to new problems without preparation, develop confidence in your ability to to meet situations as they arrise.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://74858]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2024-04-19 10:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found