[Tutor] another couple of questions

Deirdre Saoirse deirdre@deirdre.net
Thu, 3 Jun 1999 16:51:45 -0400 (EDT)


On Thu, 3 Jun 1999, Todd Martin wrote:

> Now I have this shell script that I use for dealing with IP address's and
> what it does essentially is takes an IP, breaks it down to its 4 octets like
> 255.255.255.255 and stores each octet into its own variable, like $OCT1, $OCT2,
> $OCT3, and $OCT4. By doing that I can add and subtract from $OCT4 and glue it 
> all bake together again.
> 
> So if I have 111.111.111.111 and I want to give a list of usuable IP's for a
> customer I can just add to $OCT4 and produce 111.111.111.112 etc......
> 
> How can I do this in python?

There are several ways of doing this, I'll give an example:

ipaddr = '111.111.111.111'
a = string.split(ipaddr, '.') # split the string on the periods
b = int(a[3])
for i in xrange(b, 255):
 a[3] = string.zfill(i, 3)
 newip = string.join(a, '.')
 print newip

_Deirdre   *   http://www.linuxcabal.org   *   http://www.deirdre.net
"I enjoy playing whack-a-mole with my mind" -- Rick Moen