30

I have been handed the task of creating a function in python (3.1) that will take a CIDR notation and return the list of possible ip addresses. I have looked around python.org and found this: http://docs.python.org/dev/py3k/library/ipaddr.html

but i haven't seen anything that will fill this need... I would be very grateful for any assistance anyone cares to kick my way. thanks in advance. :-)

skaffman
  • 398,947
  • 96
  • 818
  • 769
MadSc13ntist
  • 19,820
  • 8
  • 25
  • 19
  • 6
    Might want to go back and accept some answers to your questions if they were helpful... – TM. Dec 21 '09 at 21:52
  • I have used IPy (http://c0re.23.nu/c0de/IPy/) on Python 2.x, and now am in the process of porting it to Python 3. I will post a link as an answer once I am done. – AJ. Dec 21 '09 at 22:26
  • @AJ: IPy is also a good choice, but I recommended netaddr because I like it better. :) – jathanism Dec 22 '09 at 01:10
  • Update: I have submitted a new version of IPy to its current maintainer for review. Given the holidays, I'm not sure when this will be released, but I will post a link once it is. – AJ. Dec 24 '09 at 19:21

8 Answers8

71

In Python 3 as simple as

>>> import ipaddress
>>> [str(ip) for ip in ipaddress.IPv4Network('192.0.2.0/28')]
['192.0.2.0', '192.0.2.1', '192.0.2.2',
'192.0.2.3', '192.0.2.4', '192.0.2.5',
'192.0.2.6', '192.0.2.7', '192.0.2.8',
'192.0.2.9', '192.0.2.10', '192.0.2.11',
'192.0.2.12', '192.0.2.13', '192.0.2.14',
'192.0.2.15']
Petr Javorik
  • 1,695
  • 19
  • 25
  • 1
    this works directly on the python3 terminal but how about in a .py file?? – Tony Jan 04 '21 at 18:25
  • @Tony The first two lines of this answer can go into a .py file, minus the >>> at the head of each line. In the second line, you should assign the list to a variable which you can then view or do other operations on. If you put [str(ip) for ip in ipaddress.IPv4Network('192.0.2.0/28')] as-is into a file and then run it, there will be no output. But assign it to a variable like ip_list = [str(ip) for ip in ipaddress.IPv4Network('192.0.2.0/28')], then you can print(ip_list) for example to see the list. – CptSupermrkt Feb 11 '21 at 05:16
  • 3
    Please note that this answer provides a list of addresses which include both the network address (192.0.2.0) and the broadcast address (192.0.2.15) which are not usable for assigning to a host. This is correct behavior for Python, but could lead to confusion with other stakeholders if they are not familiar with networking. – iLoveTux Oct 12 '21 at 22:43
51

If you aren't married to using the built-in module, there is a project called netaddr that is the best module I have used for working with IP networks.

Have a look at the IP Tutorial which illustrates how easy it is working with networks and discerning their IPs. Simple example:

>>> from netaddr import IPNetwork
>>> for ip in IPNetwork('192.0.2.0/23'):
...    print '%s' % ip
...
192.0.2.0
192.0.2.1
192.0.2.2
192.0.2.3
...
192.0.3.252
192.0.3.253
192.0.3.254
192.0.3.255
jathanism
  • 33,067
  • 9
  • 68
  • 86
  • interesting but how is the mapping done? is there a deterministic formula that maps the CIDR networks to the single IP addresses? Or one needs a database to do the mapping? – ℕʘʘḆḽḘ Jun 20 '18 at 12:27
13

I would prefer to do a little math rather than to install an external module, no one has the same taste with me?

#!/usr/bin/env python
# python cidr.py 192.168.1.1/24

import sys, struct, socket

(ip, cidr) = sys.argv[1].split('/')
cidr = int(cidr) 
host_bits = 32 - cidr
i = struct.unpack('>I', socket.inet_aton(ip))[0] # note the endianness
start = (i >> host_bits) << host_bits # clear the host bits
end = start | ((1 << host_bits) - 1)

# excludes the first and last address in the subnet
for i in range(start, end):
    print(socket.inet_ntoa(struct.pack('>I',i)))
jfly
  • 7,715
  • 3
  • 35
  • 65
  • 1
    Note that this excludes the broadcast address, which may or may not be what you want. Remove the `-1` when computing "end" to include it. – Peter Feb 01 '18 at 15:38
  • @Peter, removing the `-1` only works on even IP addresses (e.g. where the 4th octet ends in an even number). Add `end += 1` as the line just after subtracting 1. – rabidang3ls Oct 04 '18 at 20:42
  • 1
    @jfky could you please explain this math? – drjackild Dec 16 '20 at 14:35
3

Have you checked out iptools? It seems to be a fairly good fit.

Hank Gay
  • 70,339
  • 36
  • 160
  • 222
  • Given that it seems to not support Python 2.3, it would probably require at least a nominal amount of effort to support Python 3.1. – ephemient Dec 21 '09 at 20:00
  • 2
    I've updated iptools to support Python 2.3, 2.5, 2.6 and 3.1. – bd808 Jan 01 '10 at 06:40
2

We can get this by using ipaddress lib of Python if you are not interested in playing with python logics. Else above solutions are enough.

import ipaddress
  
def get_ip_from_subnet(ip_subnet):

    ips= ipaddress.ip_network(ip_subnet)
    ip_list=[str(ip) for ip in ips]
    return ip_list

ip_subnet= "192.168.2.0/24"
print(get_ip_from_subnet(ip_subnet))
Sachin
  • 1,460
  • 17
  • 24
1

It's not in the documentation, but browsing the source suggests that ipaddr implements __iter__ and iterhosts, which is exactly what you want.


Err, nevermind.

  1. It looks like ipaddr.py was added to stdlib in 3.1 beta, but removed by 3.1 rc.
  2. I was looking at the sources from the original ipaddr.py, which seems to have evolved separately from the copy at python.org.

You could just bundle the latter.

ephemient
  • 198,619
  • 38
  • 280
  • 391
0

Below code will generate range of IPs on providing IP and subnet. Expand the CIDR notation like(255.255.255.0)

from netaddr import *

def getFirstIp(ipAddress,subnet):
  ipBin = IPNetwork(ipAddress).ip.bits().split('.')
  subBin = IPNetwork(subnet).ip.bits().split('.')
  zipped = zip(ipBin,subBin)
  netIdList = []
  for octets in zipped:
    netIdList.append(''.join(str(b) for b in (map((lambda x: int(x[0])*int(x[1])),zip(list(octets[0]),list(octets[1]))))))
  firstIp = ''
  firstIp = '.'.join(str(int(oct,2)) for oct in netIdList)
  return firstIp


def getLastIp(ipAddress,subnet):
  ipBin = IPNetwork(ipAddress).ip.bits().split('.')
  subBin = IPNetwork(subnet).ip.bits().split('.')
  #print ipBin
  #print subBin
  revsubBin = []
  for octets in subBin:
    revB = ''.join('1' if(b == '0') else '0' for b in octets)
    revsubBin.append(revB)
  zipped = zip(ipBin,revsubBin)
  netIdList = []
  for octets in zipped:
    netIdList.append(''.join(str(b) for b in (map((lambda x: 0 if(int(x[0]) == 0 and int(x[1]) == 0) else 1),zip(list(octets[0]),list(octets[1]))))))
  #print netIdList
  lastIp = ''
  lastIp = '.'.join(str(int(oct,2)) for oct in netIdList)
  return lastIp

def getRangeOfIps(firstIp,lastIp):
  start= int(IPAddress(firstIp))
  end = int(IPAddress(lastIp))
  ipList = []
  for ip in range(start,end+1):
    ipList.append(str(IPAddress(ip)))
  return ipList

def manipulateIP():
 firstIp = getFirstIp(ipAddress,subnet)
 lastIp = getLastIp(ipAddress,subnet)
 ipList = getRangeOfIps(firstIp,lastIp)  
 print ipList 
shyamtg
  • 23
  • 8
-3

Generate all Public IP Addresses given a CIDR

https://github.com/stephenlb/geo-ip will generate a list of Valid IP Public Addresses including Localities.

'1.0.0.0/8' to '191.0.0.0/8' are the valid public IP Address range exclusive of the reserved Private IP Addresses.

IP Generator

Generates a JSON dump of IP Addresses and associated Geo information. Note that the valid public IP Address range is from '1.0.0.0/8' to '191.0.0.0/8' excluding the reserved Private IP Address ranges shown lower down in this readme.

docker build -t geo-ip .
docker run -e IPRANGE='54.0.0.0/30' geo-ip               ## a few IPs
docker run -e IPRANGE='54.0.0.0/26' geo-ip               ## a few more IPs
docker run -e IPRANGE='54.0.0.0/16' geo-ip               ## a lot more IPs
docker run -e IPRANGE='0.0.0.0/0'   geo-ip               ## ALL IPs ( slooooowwwwww )
docker run -e IPRANGE='0.0.0.0/0'   geo-ip > geo-ip.json ## ALL IPs saved to JSON File
docker run geo-ip 

A little faster option for scanning all valid public addresses:

for i in $(seq 1 191); do \
    docker run -e IPRANGE="$i.0.0.0/8" geo-ip; \
    sleep 1; \ 
done

This prints less than 4,228,250,625 JSON lines to STDOUT. Here is an example of one of the lines:

{"city": "Palo Alto", "ip": "0.0.0.0", "longitude": -122.1274,
 "continent": "North America", "continent_code": "NA",
 "state": "California", "country": "United States", "latitude": 37.418,
 "iso_code": "US", "state_code": "CA", "aso": "PubNub",
 "asn": "11404", "zip_code": "94107"}

Private and Reserved IP Range

The dockerfile in the repo above will exclude non-usable IP addresses following the guide from the wikipedia article: https://en.wikipedia.org/wiki/Reserved_IP_addresses

MaxMind Geo IP

The dockerfile imports a free public Database provided by https://www.maxmind.com/en/home

Stephen Blum
  • 6,498
  • 2
  • 34
  • 46