Created
December 31, 2021 03:10
-
-
Save CCCougar/8d12a638f2705c718da7f9b7ab00daf2 to your computer and use it in GitHub Desktop.
Generate an IP range list in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# author: https://tkit.dev/2011/09/11/how-to-generate-an-ip-range-list-in-python/ | |
def ipRange(start_ip, end_ip): | |
start = list(map(int, start_ip.split("."))) | |
end = list(map(int, end_ip.split("."))) | |
temp = start | |
ip_range = [] | |
ip_range.append(start_ip) | |
while temp != end: | |
start[3] += 1 | |
for i in (3, 2, 1): | |
if temp[i] == 256: | |
temp[i] = 0 | |
temp[i-1] += 1 | |
ip_range.append(".".join(map(str, temp))) | |
return ip_range | |
# sample usage | |
ip_range = ipRange("192.168.1.0", "192.171.3.25") | |
for ip in ip_range: | |
print(ip) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment