2011-12-01 136 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