Il codice sottostante genererà un intervallo di IP su IP e subnet. Espandere la notazione CIDR come (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
potrebbe desiderare di tornare indietro e accettare alcune risposte alle tue domande se fossero utili ... –
Ho usato IPY (http://c0re.23.nu/c0de/IPy/) su Python 2. x, e ora sono in procinto di portarlo su Python 3. Pubblicherò un link come risposta una volta che ho finito. –
@AJ: IPy è anche una buona scelta, ma ho raccomandato netaddr perché mi piace di più. :) Aggiornamento – jathanism