Return List of IP Addresses from a IP Range in Python or PHP
def ips(start, end):
    import socket, struct
    start = struct.unpack('>I', socket.inet_aton(start))[0]
    end = struct.unpack('>I', socket.inet_aton(end))[0]
    return [socket.inet_ntoa(struct.pack('>I', i)) for i in range(start, end)]

Now lets test it:

ips('1.2.3.4', '1.2.4.5')

In PHP:

$x = ip2long('10.0.1.1'); $y = ip2long('10.0.1.6'); for ($i=$x;$i<=$y;$i++) { echo long2ip($i) . "\n"; }

Leave a Reply

Your email address will not be published. Required fields are marked *