Python 3 only, for IPv4, same idea with @User but use new Python3 standard library: ipaddress
IPv4 is represented by 4 bytes. So next IP is actually next number, a range of IPs can be represented as a range of integer numbers.
0.0.0.1 is 1
0.0.0.2 is 2
...
0.0.0.255 is 255
0.0.1.0 is 256
0.0.1.1 is 257
By code (ignore the In []: and Out []:)
In [68]: from ipaddress import ip_address
In [69]: ip_address('0.0.0.1')
Out[69]: IPv4Address('0.0.0.1')
In [70]: ip_address('0.0.0.1').packed
Out[70]: b'\x00\x00\x00\x01'
In [71]: int(ip_address('0.0.0.1').packed.hex(), 16)
Out[71]: 1
In [72]: int(ip_address('0.0.1.0').packed.hex(), 16)
Out[72]: 256
In [73]: int(ip_address('0.0.1.1').packed.hex(), 16)
Out[73]: 257
ip.packed.hex()
returns the hexadecimal form of 4 bytes, as it is in
hexadecimal, it is shorter (e.g: 0xff hex == 255 decimal == 0b11111111 binary),
and thus, often used for representing bytes. int(hex, 16)
returns integer value
corresponding to the hex value as it is more human friendly, and can be used as input for ip_address
.
from ipaddress import ip_address
def ips(start, end):
'''Return IPs in IPv4 range, inclusive.'''
start_int = int(ip_address(start).packed.hex(), 16)
end_int = int(ip_address(end).packed.hex(), 16)
return [ip_address(ip).exploded for ip in range(start_int, end_int)]
ips('192.168.1.240', '192.168.2.5')
Returns:
['192.168.1.240',
'192.168.1.241',
'192.168.1.242',
'192.168.1.243',
'192.168.1.244',
'192.168.1.245',
'192.168.1.246',
'192.168.1.247',
'192.168.1.248',
'192.168.1.249',
'192.168.1.250',
'192.168.1.251',
'192.168.1.252',
'192.168.1.253',
'192.168.1.254',
'192.168.1.255',
'192.168.2.0',
'192.168.2.1',
'192.168.2.2',
'192.168.2.3',
'192.168.2.4']
IPv4
addresses? Or are thereIPv6
s in there too? – Perfectionism