I am trying to create a script that generates a list of IP addresses based on a users input for a start and end IP range. For example, they could enter 192.168.1.25 & 1
Remember that a dotted IPv4-address "x.x.x.x" is nothing more than a human-readable representation of a 32-bit integer. Using this, you can generate the ranges like this:
def undotIPv4 (dotted):
return sum (int (octet) << ( (3 - i) << 3) for i, octet in enumerate (dotted.split ('.') ) )
def dotIPv4 (addr):
return '.'.join (str (addr >> off & 0xff) for off in (24, 16, 8, 0) )
def rangeIPv4 (start, stop):
for addr in range (undotIPv4 (start), undotIPv4 (stop) ):
yield dotIPv4 (addr)
for x in rangeIPv4 ('1.2.3.4', '1.2.4.22'):
print (x)