Python 3: create a list of possible ip addresses from a CIDR notation

后端 未结 7 878
花落未央
花落未央 2020-12-08 07:57

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.

相关标签:
7条回答
  • 2020-12-08 08:20

    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']
    
    0 讨论(0)
  • 2020-12-08 08:22

    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
    
    0 讨论(0)
  • 2020-12-08 08:23

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

    0 讨论(0)
  • 2020-12-08 08:26

    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

    0 讨论(0)
  • 2020-12-08 08:32

    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.

    0 讨论(0)
  • 2020-12-08 08:42

    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 
    
    0 讨论(0)
提交回复
热议问题