How to generate an IP range list in Python?

September 11, 2011

Here’s a short Python snippet that generates a list of IP addresses based on IP range. Nothing fancy, but could come in handy if you ever need it somewhere in your code. Enjoy!

def ipRange(start_ip, end_ip):
  start = list(map(int, start_ip.split(".")))
  end = list(map(int, end_ip.split(".")))
  temp = start
  ip_range = []

  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("", "")
for ip in ip_range:

Written by Tihomir Kit, a senior software engineer @Dovetail.
🖖 Stay curious. @GitHub