2013-07-14 40 views
1

比方說,我有一個文本文件包含了這樣一串IP範圍:如何從Python中的IP範圍列表中生成所有可能的IP?

x.x.x.x-y.y.y.y 
x.x.x.x-y.y.y.y 
x.x.x.x-y.y.y.y 
x.x.x.x-y.y.y.y 
x.x.x.x-y.y.y.y 

X.X.X.X是啓動值和y.y.y.y是範圍終值。

如何將這些ip範圍轉換爲python中新文本文件中的所有可能的IP地址?

PS:這個問題與我以前的任何問題都不一樣。我在之前的問題中問過「如何從cidr符號生成所有可能的ips」。但在這裏我問「如何從IP範圍列表生成」。這些是不同的事情。

+0

難道只是一個'IPv4'地址的文件?或者那裏還有'IPv6's? – will

+0

@只會IPv4 – Leadri

回答

8

該函數返回的所有IP地址就像從開始到結束:

def ips(start, end): 
    import socket, struct 
    start = struct.unpack('>I', socket.inet_aton(start))[0] 
    end = struct.unpack('>I', socket.inet_aton(end))[0] 
    return [socket.inet_ntoa(struct.pack('>I', i)) for i in range(start, end)] 

這些積木打造它自己:

>>> import socket, struct 
>>> ip = '0.0.0.5' 
>>> i = struct.unpack('>I', socket.inet_aton(ip))[0] 
>>> i 
5 
>>> i += 1 
>>> socket.inet_ntoa(struct.pack('>I', i)) 
'0.0.0.6' 

例子:

ips('1.2.3.4', '1.2.4.5') 
['1.2.3.4', '1.2.3.5', '1.2.3.6', '1.2.3.7', ..., '1.2.3.253', '1.2.3.254', '1.2.3.255', '1.2.4.0', '1.2.4.1', '1.2.4.2', '1.2.4.3', '1.2.4.4'] 

從文件中讀取

在你的情況,你可以從文件中這樣寫着:

with open('file') as f: 
    for line in f: 
     start, end = line.strip().split('-') 
     # .... 
+0

感謝您的回答,但我是新來的蟒蛇,我不明白如何使其工作。並且可以顯示如何從文本文件中讀取iprange list並創建一個包含所有可能的ips的新文本文件? – Leadri

+0

爲什麼這不被標記爲接受答案?從Python中的文件中讀取文本是一個基本的任務,你可以找到數百個其他職位,可以告訴你如何完成這一點。有關問題的原始問題在這裏得到了明確解答,其餘部分您需要使用google/duckduckgo進行處理。 – r2d2oid

1

試試這個:

def ip_range(start_ip,end_ip): 
start = list(map(int,start_ip.split('.'))) 
end = list(map(int,end_ip.split('.'))) 
iprange=[] 
while start!=list(map(int,end_ip.split('.'))): 
    for i in range(len(start)-1,-1,-1): 
     if start[i]<255: 
      start[i]+=1 
      break 
     else: 
      start[i]=0 
    iprange.append('.'.join(map(str,start))) 
return iprange 
0

的Python 3只,IPv4的,同樣的想法與@user但使用新的Python3標準庫:ipaddress

IPv4由4個字節表示。所以下一個IP實際上是下一個數字,一系列IP可以表示爲整數的範圍。

0.0.0.1是1

0.0.0.2是2

...

0.0.0.255是255

0.0.1.0是256

0.0.1.1是257

按代碼(忽略In []:和Out [] :)

In [68]: from ipaddress import ip_address 

In [69]: ip_address('0.0.0.1') 
Out[69]: IPv4Address('0.0.0.1') 

In [70]: ip_address('0.0.0.1').packed 
Out[70]: b'\x00\x00\x00\x01' 

In [71]: int(ip_address('0.0.0.1').packed.hex(), 16) 
Out[71]: 1 

In [72]: int(ip_address('0.0.1.0').packed.hex(), 16) 
Out[72]: 256 

In [73]: int(ip_address('0.0.1.1').packed.hex(), 16) 
Out[73]: 257 

ip.packed.hex()返回的4個字節的十六進制形式,因爲它是在十六進制 ,它是較短的(例如:0xff的十六進制== 255十進制== 0b11111111二進制), 並且因此,經常被用於表示字節。 int(hex, 16)返回與十六進制值相對應的整數值 ,因爲它更爲人性化,並可用作ip_address的輸入。

from ipaddress import ip_address 

def ips(start, end): 
    '''Return IPs in IPv4 range, inclusive.''' 
    start_int = int(ip_address(start).packed.hex(), 16) 
    end_int = int(ip_address(end).packed.hex(), 16) 
    return [ip_address(ip).exploded for ip in range(start_int, end_int)] 


ips('192.168.1.240', '192.168.2.5') 

返回:

['192.168.1.240', 
'192.168.1.241', 
'192.168.1.242', 
'192.168.1.243', 
'192.168.1.244', 
'192.168.1.245', 
'192.168.1.246', 
'192.168.1.247', 
'192.168.1.248', 
'192.168.1.249', 
'192.168.1.250', 
'192.168.1.251', 
'192.168.1.252', 
'192.168.1.253', 
'192.168.1.254', 
'192.168.1.255', 
'192.168.2.0', 
'192.168.2.1', 
'192.168.2.2', 
'192.168.2.3', 
'192.168.2.4']