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

后端 未结 7 905
花落未央
花落未央 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: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 
    

提交回复
热议问题