IP Math anyone?

Jay O'Connor joconnor at cybermesa.com
Mon Feb 26 22:44:52 CET 2001


Timothy Grant wrote:

> Hi all,
>
> I checked Parnassus, but couldn't find anything, so I'll ask
> here. Has anyone done anything with IP math (e.g.,
> 192.168.0.254 + 1 = 192.168.1.1).

You could probably do it by converting the IP address to a number, do
the math, and convert back.  I've seen algorithms for doing this.

 Given a.b.c.d for an IP address you can convert it to the proper value
using:

a(256^3) + b(256^2) + c(256) + d

So 192.168.0.254 = (192 * long((256 **3))) + (168 * (256 ** 2)) + (0 *
256) + 254 = 3232235774L

You need the long() in there to avoid an integer multiplication error.

You might be able to use '3232235774' directly, or convert back to an ip
address

Take care,
Jay






More information about the Python-list mailing list