我被交付任务,在Python(3.1)中创建一个函数,该函数将接收CIDR表示法并返回可能的IP地址列表。我在python.org上进行了搜索,并找到了以下内容:http://docs.python.org/dev/py3k/library/ipaddr.html
但是我没有看到任何可以满足这个需求的东西......如果有人能提供任何帮助,我将非常感激。先行致谢。:-)
我被交付任务,在Python(3.1)中创建一个函数,该函数将接收CIDR表示法并返回可能的IP地址列表。我在python.org上进行了搜索,并找到了以下内容:http://docs.python.org/dev/py3k/library/ipaddr.html
但是我没有看到任何可以满足这个需求的东西......如果有人能提供任何帮助,我将非常感激。先行致谢。:-)
在Python 3中就这么简单
>>> 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']
我宁愿做一点数学,也不想安装一个外部模块,难道没有跟我一样的人吗?
#!/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)))
你是否查看过iptools
?它似乎非常适合。
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))
虽然文档中没有提到,但浏览源代码可以发现ipaddr
实现了__iter__
和iterhosts
,这正是您所需要的。
哦,没事了。
ipaddr.py
在 3.1 beta 版本中被添加到标准库中,但在 3.1 rc 版本中被删除了。你可以只打包后者。
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
https://github.com/stephenlb/geo-ip会生成一个包括地理位置在内的有效公共IP地址列表。
'1.0.0.0/8'
到'191.0.0.0/8'
是有效的公共IP地址范围,不包括保留的私有IP地址。
生成IP地址和相关地理信息的JSON转储。请注意,有效的公共IP地址范围从'1.0.0.0/8'
到'191.0.0.0/8'
,不包括下面自述中显示的保留的私有IP地址范围。
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
扫描所有有效公共地址的更快选项:
for i in $(seq 1 191); do \
docker run -e IPRANGE="$i.0.0.0/8" geo-ip; \
sleep 1; \
done
这将少于4,228,250,625个JSON行打印到STDOUT。以下是其中一行的示例:
{"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"}
上述存储库中的dockerfile将根据维基百科文章的指南排除不可用的IP地址: https://en.wikipedia.org/wiki/Reserved_IP_addresses
该dockerfile导入了由https://www.maxmind.com/en/home提供的免费公共数据库。