27

我接到了在 python (3.1) 中创建一个函数的任务,该函数将采用 CIDR 表示法并返回可能的 IP 地址列表。我查看了 python.org 并发现了这个: http ://docs.python.org/dev/py3k/library/ipaddr.html

但我还没有看到任何可以满足这种需求的东西......我将非常感谢任何人关心我的方式的任何帮助。提前致谢。:-)

4

8 回答 8

58

在 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']
于 2018-12-06T09:37:31.327 回答
51

如果您不喜欢使用内置模块,那么有一个名为netaddr的项目是我用于处理 IP 网络的最佳模块。

看一下IP 教程,它说明了使用网络和识别它们的 IP 是多么容易。简单的例子:

>>> 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
于 2009-12-21T21:50:12.343 回答
11

我宁愿做一点数学而不是安装外部模块,没有人和我一样的品味吗?

#!/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)))
于 2017-05-18T09:12:22.300 回答
2

你检查了iptools吗?这似乎是一个相当不错的选择。

于 2009-12-21T19:43:28.390 回答
1

它不在文档中,但浏览源代码表明ipaddr实现__iter__and iterhosts,这正是您想要的。


错了,没关系。

  1. 它看起来像是ipaddr.py在 3.1 beta 中添加到 stdlib,但被 3.1 rc 删除。
  2. 我正在查看原始ipaddr.py的来源,它似乎与 python.org 上的副本分开发展。

你可以捆绑后者。

于 2009-12-21T20:06:42.243 回答
0

如果您对玩 Python 逻辑不感兴趣,我们可以使用 Python 的 ipaddress lib 来获得它。其他上述解决方案就足够了。

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))
于 2021-06-17T13:12:46.330 回答
0

下面的代码将在提供 IP 和子网时生成 IP 范围。扩展 CIDR 表示法,如 (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 
于 2016-09-23T06:06:35.823 回答
-3

生成给定 CIDR 的所有公共 IP 地址

https://github.com/stephenlb/geo-ip将生成一个有效 IP 公共地址列表,包括地区。

'1.0.0.0/8'to'191.0.0.0/8'是有效的公共 IP 地址范围,不包括保留的私有 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"}

私有和保留 IP 范围

上面 repo 中的 dockerfile 将按照维基百科文章中的指南排除不可用的 IP 地址: https ://en.wikipedia.org/wiki/Reserved_IP_addresses

MaxMind 地理 IP

dockerfile 导入https://www.maxmind.com/en/home提供的免费公共数据库

于 2019-03-01T21:07:58.057 回答