2011-12-01 135 views
2

我有一个IP地址范围:如何获取IP地址列表?

1.48.0.0 - 1.51.255.255 

如何获得IP地址的列表?

+5

我想将它们转换成单一的整数,遍历范围,并相互转换价值回到一个IP地址。 – NPE

+0

如果您遵循@ aix的建议,这可能会有所帮助。 http://snipplr.com/view/14807/ – Dogbert

回答

2
from struct import * 
from socket import * 

for ip in xrange(unpack('!I',inet_pton(AF_INET,"1.47.0.0"))[0],unpack('!I',inet_pton(AF_INET,"1.51.255.255"))[0]): 
    print inet_ntop(AF_INET,pack('!I',ip)); 

f = unpack('!I',inet_pton(AF_INET,"1.47.0.0"))[0] 
l = unpack('!I',inet_pton(AF_INET,"1.51.255.255"))[0] 
while f < l: 
    print inet_ntop(AF_INET,pack('!I',f)); 
    f = f + 1 

这样也就比较容易走通IPv6地址为好,但我不会推荐它,因为IPv6的空间浩瀚的。

2

如果我理解正确...

start = [1,48,0,0] 
end = [1,51,255,255] 

def generate_range(start, end): 
    cur = start 

    while cur < end:  
     cur[3] = int(cur[3]) + 1 

     for pos in range(len(cur)-1, -1, -1): 
      if cur[pos] == 255: 
       cur[pos] = 0 
       cur[pos-1] = int(cur[pos-1]) + 1 

     yield '.'.join("{}".format(cur[i]) for i in range(0,len(cur))) 

for x in generate_range(start, end): 
    print (x) 

是丑陋的,但会做的工作。
这将创建一个所有可能的ip值的生成器序列。

意识到,这是python 3.0代码,以获得最佳的结果python 2.X


编辑使用xrange: 算法的最后一个版本有一个bug,这个版本不

0

我已经实现它使用简单的嵌套while循环。 如果您发现任何错误,请及时通知我。

a=1 
b=48 

while b <= 51: 
    c=0 
    d=0 
    while c <= 255: 
     d=0 
     while d <= 255: 
      print str(a)+"."+str(b)+"."+str(c)+"."+str(d) 
      d += 1 
     c += 1 
    b += 1 
0

您可以将此代码扩展为4个字段的IP形式。

现在你可以使用ipRange*

startIP='0.0' 
endIP = '10.10' 
ipRange = [] 
for i in range(int(startIP.split('.')[-1]), int(endIP.split('.')[-1])): 
    for j in range(int(startIP.split('.')[-2]), int(endIP.split('.')[-2])): 
     ipRange.append(str(i)+ ''.join('.') + str(j)) 

结果是:

$ ipRange 
['0.0', '0.1', '0.2', '0.3', '0.4', '0.5', '0.6', '0.7', '0.8', '0.9', '1.0', '1.1', '1.2', '1.3', '1.4', '1.5', '1.6', '1.7', '1.8', '1.9', '2.0', '2.1', '2.2', '2.3', '2.4', '2.5', '2.6', '2.7', '2.8', '2.9', '3.0', '3.1', '3.2', '3.3', '3.4', '3.5', '3.6', '3.7', '3.8', '3.9', '4.0', '4.1', '4.2', '4.3', '4.4', '4.5', '4.6', '4.7', '4.8', '4.9', '5.0', '5.1', '5.2', '5.3', '5.4', '5.5', '5.6', '5.7', '5.8', '5.9', '6.0', '6.1', '6.2', '6.3', '6.4', '6.5', '6.6', '6.7', '6.8', '6.9', '7.0', '7.1', '7.2', '7.3', '7.4', '7.5', '7.6', '7.7', '7.8', '7.9', '8.0', '8.1', '8.2', '8.3', '8.4', '8.5', '8.6', '8.7', '8.8', '8.9', '9.0', '9.1', '9.2', '9.3', '9.4', '9.5', '9.6', '9.7', '9.8', '9.9'] 
0

获取全部有效的IP地址在给定范围内

我会用这一直是一个模块专为处理IP地址而编写。一个简单的解决方案是由netaddrpip install netaddr)提供的。以下是将打印在给定范围内的所有IP地址(例如1.48.0.0 - 1.51.255.255)为例:


#!/usr/bin/env python 

"""Get a list of IP Addresses in a range (CIDR Notation).""" 

import sys # Using for cleanly exiting program. 

try: 
    # Pythonic manipulation of IPv4, IPv6, CIDR, EUI and MAC network addresses. 
    from netaddr import IPNetwork 

except ImportError: 
    sys.exit(
     """ 
     Missing 'netaddr' module, get it here: 

     https://pypi.python.org/pypi/netaddr 
     """ 
    ) 


def get_ips(cidr_string): 
    """Returns all IPv4 Addresses in a given CIDR Range 
    :param cidr_string: IP Address Range in CIDR notation 
    :returns: list of IP Addresses 
    :rtype: list 
    """ 

    # Cast to list for simpler manipulation. 
    ip_list = list(IPNetwork(cidr_string)) 

    return ip_list 

# Get IPs in Range '1.48.0.0 - 1.51.255.255' (Note the CIDR Notation). 
ip_list = get_ips('1.48.0.0/14') 

for ip_address in ip_list: 

    print ip_address