Let's say I have a text file contains a bunch of ip ranges like this:
x.x.x.x-y.y.y.y
x.x.x.x-y.y.y.y
x.x.x.x-y.y.y.y
x.x.x.x-y.y.y.y
x.x.x.x-y.y.y.y
x.x.x.x is start value and y.y.y.y is end value of range.
How can I convert these ip ranges to all possible IPs in a new text file in python?
PS: This question is not same as any of my previous questions. I asked "how to generate all possible ips from cidr notations" in my previous question. But in here I ask "how to generate from ip range list". These are different things.
try This:
This function returns all ip addresses like from start to end:
These are the building blocks to build it on your own:
Example:
Read from file
In your case you can read from a file like this:
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 []:)
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 forip_address
.Returns: