Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: ip address calculations

by ariels (Curate)
on May 06, 2002 at 11:45 UTC ( [id://164285]=note: print w/replies, xml ) Need Help??


in reply to ip address calculations

For your first code, Perl 5.6.1 lets you do this kind of niftiness:

my ($s,$e) = unpack 'N2',1.2.3.4 . 1.2.4.2; printf("%vd\n",pack 'N',$_) for ($s..$e)
If you use this code, though (complete with whitespace sensitivity that makes Python look like a Good Idea), you might well be shot. Replace the unpacks with a more direct translation of dotted quadruplets into ints, e.g.
sub ip_to_num { my $ip = shift; my $n; $n = $n*256+$_ for (split /\./,$ip); $n }

For the second question (and, indeed, for the first), it would be best to post some code, or at the very least some sample input/output.

Replies are listed 'Best First'.
Re: Re: ip address calculations
by tachyon (Chancellor) on May 06, 2002 at 12:01 UTC
    my ($s,$e) = unpack 'N2',172.16.0.1 . 172.17.0.0; printf("%vd\n",pack 'N',$_) for ($s..$e) __DATA__ Range iterator outside integer range at test.pl line 2.

    This will fail on "real" addresses (that exceed integer range) as shown.

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://164285]
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: (4)
As of 2024-04-24 18:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found