[networking] Convert subnet mask <=> mask length

Discussion in 'Perl Misc' started by 187, Jul 29, 2004.

  1. 187

    187 Guest

    For example, how can I go from 255.255.252.0 to /22 (as in, for example,
    150.10.10.10/22) and vice versa.

    I've bane up and down both www.google.com and groups.google.com, tried
    searching by perl group, and even globally, and nothing, just online
    calculators. Calculators are nice tools, but I really want to know *how*
    it's done :)

    Thanks.
    187, Jul 29, 2004
    #1
    1. Advertising

  2. 187

    Jim Liebgott Guest

    On Wed, 28 Jul 2004 16:18:43 -0700, 187 <>
    wrote:


    $subnet='255.255.252.0';
    @octets=split(/\./,$subnet);
    $bits=40;
    do
    {
    $last_octet=pop(@octets);
    $bits-=8;
    }
    while (!$last_octet && @octets);
    if (@octets)
    {
    while (!($last_octet%2))
    {
    $bits--;
    $last_octet>>=1;
    }
    }
    print $bits,"\n";


    > For example, how can I go from 255.255.252.0 to /22 (as in, for example,
    > 150.10.10.10/22) and vice versa.
    >
    > I've bane up and down both www.google.com and groups.google.com, tried
    > searching by perl group, and even globally, and nothing, just online
    > calculators. Calculators are nice tools, but I really want to know *how*
    > it's done :)
    >
    > Thanks.
    >
    >




    --
    Using Opera's revolutionary e-mail client: http://www.opera.com/m2/
    Jim Liebgott, Jul 29, 2004
    #2
    1. Advertising

  3. 187

    Bart Lateur Guest

    Warren Block wrote:

    >use Socket;
    >
    >print '/', unpack('%32b*', inet_aton($ARGV[0])), "\n";


    That's just beautiful.

    It does depend on the fact that you assume the input will match the
    binary pattern /^1*0*$/, and you can't be sure...

    My version makes sure it does. It also follows a more conventional
    route.

    #!/usr/local/bin/perl -wl
    use Socket;

    my $ip = '255.255.252.0';

    if(unpack('B*', inet_aton($ip)) =~ /^(1*)0*$/) {
    print "/" . length $1;
    } else {
    print "No match";
    }

    --
    Bart.
    Bart Lateur, Jul 29, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Glenn M

    Get Requesting Clients Subnet Mask

    Glenn M, Oct 12, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    421
    John Saunders
    Oct 12, 2004
  2. Pierre Rouleau
    Replies:
    0
    Views:
    390
    Pierre Rouleau
    Dec 10, 2003
  3. yagru_alvarez
    Replies:
    7
    Views:
    605
    Michael Doubez
    Jun 6, 2011
  4. David Corticchiato

    Get the subnet mask

    David Corticchiato, Dec 19, 2006, in forum: Ruby
    Replies:
    3
    Views:
    174
    Shiwei Zhang
    Jan 12, 2007
  5. kielhd
    Replies:
    3
    Views:
    332
    John W. Krahn
    Nov 9, 2003
Loading...

Share This Page