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

Increment of ip address

by mosh (Scribe)
on Jun 16, 2005 at 15:52 UTC ( #467330=perlquestion: print w/ replies, xml ) Need Help??
mosh has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks !

I tried to increment ip address as follows.
my $ip = '1.2.3.4' +1;
I meant that the $ip would get the value '1.2.3.5'.
Of course it wasn't work:
"Argument "1.2.3.4" isn't numeric in addition (+)..."

Do anyone have an idea how should I do it ?

Thanks,
Mosh.

Comment on Increment of ip address
Re: Increment of ip address
by davidrw (Prior) on Jun 16, 2005 at 15:57 UTC
    Try Net::IP .. note especially that it overloads the ++ operator
      There's also NetAddr::IP which also overloads ++ (and + 1)
Re: Increment of ip address
by cool_jr256 (Acolyte) on Jun 16, 2005 at 16:02 UTC
    '1.2.3.5' is being treated as a string (because of the '.'). You can split up the IP into an array, hence being able to increment any of the ip numbers:
    $ip="1.2.3.4"; @ipnum=split(/./,$ip); $ipnum[$#ipnum]++; # this increments the last digit.. # then $ip=join(".",@ipnum); # result is '1.2.3.5';
      Make sure that you split on /\./ and not just 'dot', which is any character. Also, if you increment 192.168.255.255 with that method you're not going to get what you want ... (see other responses for modules to do it for you)
Re: Increment of ip address
by monkey_boy (Curate) on Jun 16, 2005 at 16:10 UTC
    Update:
    As it has been mentioned , this does not work in many situations, please ignore my stupid ramblings .. the simple way ..
    use strict; my $ip = '1.2.3.4'; my @ip = split(/\./,$ip); $ip[3]++; print join('.',@ip) , "\n";



    This is not a Signature...
Re: Increment of ip address
by jdporter (Canon) on Jun 16, 2005 at 16:19 UTC
    Net::IP or NetAddr::IP is really the way to go... but for fun, here's a simple IP math function. (Note that it only works on IPv4 addresses.)
    use Socket; sub add_to_ip($$) { my( $ip, $add ) = @_; inet_ntoa pack( 'N', unpack('N',inet_aton $ip) + $add ) }
    You can add or subtract any amount to an IP address this way. Carry-over into the next "quad" is automatic... but that may not be what you want. :-)
    $j = add_to_ip( $i, 1 ); # simple increment $j = add_to_ip( $i, -1 ); # simple decrement $j = add_to_ip( $i, 300 ); # addition carries over into the next-to-la +st quad.
      Hi jdporter, Thanks !

      What does it mean the $$ in:
      sub add_to_ip($$)

      Mosh.

        That would be a prototype, the subject of much discussion on usefulness verging on holy war status.  The Camel Book says the intent behind prototypes "... is primarily to let you define subroutines that work like built-in functions ..." (p. 226).  In this case, it is giving a contextual clue to perl that this sub accepts two scalars.

Re: Increment of ip address
by monarch (Priest) on Jun 17, 2005 at 00:54 UTC
    In thread perl ipcalc oneliners I posted some quick math subroutines you can use for converting your IP address into a number and back for arithmetic purposes without relying on any other module.

    Try using:

    sub addrtoint { return( unpack( "N", pack( "C4", split( /[.]/,$_[0] ) ) ) ) }; sub inttoaddr { return( join( ".", unpack( "C4", pack( "N", $_[0] ) ) ) ) }; my $ipinc = addrtoint( '1.2.3.4' ); $ipinc++; print( inttoaddr( $ipinc ) . "\n" );

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://467330]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2014-09-01 14:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (13 votes), past polls