Search This Blog

Thursday, February 12, 2009

IP Address Range Function in Python

def ipAddrRange(startAddr, endAddr):
 def incrAddr(addrList):
    addrList[3] += 1
    for i in (3,2,1):
      if addrList[i] == 256:
         addrList[i] = 0
         addrList[i-1] += 1
 def asString(addrList):
     return ".".join(map(str,addrList))
 startAddrList = map(int,startAddr.split("."))
 endAddrList = map(int,endAddr.split("."))
 curAddrList = startAddrList[:]
 yield asString(curAddrList)
 for i in range(4):
     while curAddrList[i] < endAddrList[i]:
         yield asString(curAddrList)

for addr in ipAddrRange("",""):
print addr

1 comment:

Ryan said...

Just what I needed, and no modules to import. Thanks!