在python中查找下一个可用的IP地址
使用python,我需要找到下一个IP地址,给定我已经使用过的IP地址范围。所以如果我有一个IP地址列表,比如在python中查找下一个可用的IP地址,python,Python,使用python,我需要找到下一个IP地址,给定我已经使用过的IP地址范围。所以如果我有一个IP地址列表,比如 IPs = ['10.220.1.1','10.220.1.2','10.220.1.3','10.220.1.5'] 当我请求下一个IP地址时,我需要它返回“10.220.1.4”。下一个请求将返回“10.220.1.6”,依此类推。您可以使用模块使用生成器,该模块是python>=3.3中的bultin,也可以使用pip安装早期版本: In [20]: from ipaddres
IPs = ['10.220.1.1','10.220.1.2','10.220.1.3','10.220.1.5']
当我请求下一个IP地址时,我需要它返回“10.220.1.4”。下一个请求将返回“10.220.1.6”,依此类推。您可以使用模块使用生成器,该模块是python>=3.3中的bultin,也可以使用pip安装早期版本:
In [20]: from ipaddress import ip_network
In [21]: IPs = {'10.220.1.1','10.220.1.2','10.220.1.3','10.220.1.5'}
In [22]: net = ip_network(u"10.220.1.0/24")
In [23]: avail =(str(ip) for ip in net.hosts() if str(ip) not in IPs
....: )
In [24]: next(avail)
Out[24]: '10.220.1.4'
In [25]: next(avail)
Out[25]: '10.220.1.6'
您可以使用使用python>=3.3中bultin模块的生成器,也可以使用pip安装早期版本的生成器:
In [20]: from ipaddress import ip_network
In [21]: IPs = {'10.220.1.1','10.220.1.2','10.220.1.3','10.220.1.5'}
In [22]: net = ip_network(u"10.220.1.0/24")
In [23]: avail =(str(ip) for ip in net.hosts() if str(ip) not in IPs
....: )
In [24]: next(avail)
Out[24]: '10.220.1.4'
In [25]: next(avail)
Out[25]: '10.220.1.6'
如果您使用的是Python 3,则可以与生成器一起使用:
import ipaddress
def gen_ip(start, reserved):
start = int(ipaddress.IPv4Address(start))
for i in range(start + 1, int(2 ** 32) + 1):
ip = str(ipaddress.IPv4Address(i))
if ip not in reserved:
yield ip
IPs = ['10.220.1.1','10.220.1.2','10.220.1.3','10.220.1.5']
j = 0
for ip in gen_ip(min(IPs), set(IPs)):
print(ip)
j += 1
if j == 5:
break
输出:
10.220.1.4
10.220.1.6
10.220.1.7
10.220.1.8
10.220.1.9
如果您使用的是Python 3,则可以与生成器一起使用:
import ipaddress
def gen_ip(start, reserved):
start = int(ipaddress.IPv4Address(start))
for i in range(start + 1, int(2 ** 32) + 1):
ip = str(ipaddress.IPv4Address(i))
if ip not in reserved:
yield ip
IPs = ['10.220.1.1','10.220.1.2','10.220.1.3','10.220.1.5']
j = 0
for ip in gen_ip(min(IPs), set(IPs)):
print(ip)
j += 1
if j == 5:
break
输出:
10.220.1.4
10.220.1.6
10.220.1.7
10.220.1.8
10.220.1.9
创建一个基本ip对象来保存当前ip的记录并获取下一个ip
class IPObj(object):
def __init__(self, list_of_ips):
self.position = 0
self.ips = list_of_ips
self.current_ip = self.ips[self.position]
def next_ip(self, stop_iteration=False):
'''
return the next ip in the list
'''
if self.position >= len(self.ips)-1:
self.position = 0 # By default next_ip will do nothing when it reaches the end but it will throw an exception if stop_iteration==True
if stop_iteration:
raise StopIteration
self.position += 1
self.current_ip = self.ips[self.position]
return self.current_ip
def __repr__(self):
return repr(self.current_ip)
#Testing
ips = IPObj(['10.220.1.1','10.220.1.2','10.220.1.3','10.220.1.5'])
print ips
while True:
print ips.next_ip(True),
输出:
10.220.1.1,
10.220.1.2,
10.220.1.3,
10.220.1.5
回溯(最近一次呼叫最后一次):
文件“C:\Users\J10ey\workspace\SO\u Help\src\ip's.py”,第32行,在
打印ip。下一个ip(真)
文件“C:\Users\J10ey\workspace\SO\u Help\src\ip's.py”,第21行,在下一个\u ip中
提出停止迭代
停止迭代
创建一个基本ip对象来保存当前ip的记录并获取下一个ip
class IPObj(object):
def __init__(self, list_of_ips):
self.position = 0
self.ips = list_of_ips
self.current_ip = self.ips[self.position]
def next_ip(self, stop_iteration=False):
'''
return the next ip in the list
'''
if self.position >= len(self.ips)-1:
self.position = 0 # By default next_ip will do nothing when it reaches the end but it will throw an exception if stop_iteration==True
if stop_iteration:
raise StopIteration
self.position += 1
self.current_ip = self.ips[self.position]
return self.current_ip
def __repr__(self):
return repr(self.current_ip)
#Testing
ips = IPObj(['10.220.1.1','10.220.1.2','10.220.1.3','10.220.1.5'])
print ips
while True:
print ips.next_ip(True),
输出:
10.220.1.1,
10.220.1.2,
10.220.1.3,
10.220.1.5
回溯(最近一次呼叫最后一次):
文件“C:\Users\J10ey\workspace\SO\u Help\src\ip's.py”,第32行,在
打印ip。下一个ip(真)
文件“C:\Users\J10ey\workspace\SO\u Help\src\ip's.py”,第21行,在下一个\u ip中
提出停止迭代
停止迭代
将列表转换为集合,以提高性能:
used_ips = set(IPs)
现在,按照您的意愿生成您的IP,并检查它们是否包含在集合中:
for next_ip in generate_ip_numbers():
if next_ip in used_ips:
continue
print("Next ip address is:", next_ip)
将列表转换为集合,以提高性能:
used_ips = set(IPs)
现在,按照您的意愿生成您的IP,并检查它们是否包含在集合中:
for next_ip in generate_ip_numbers():
if next_ip in used_ips:
continue
print("Next ip address is:", next_ip)
如果您使用的是Python3.3(或更新版本),则可以使用该模块。子网
10.220.1.0/24
中所有主机的示例,保留的中的主机除外:
from ipaddress import IPv4Network
network = IPv4Network('10.220.1.0/24')
reserved = {'10.220.1.1', '10.220.1.2', '10.220.1.3', '10.220.1.5'}
hosts_iterator = (host for host in network.hosts() if str(host) not in reserved)
# Using hosts_iterator:
print(next(hosts_iterator)) # prints 10.220.1.4
print(next(hosts_iterator)) # prints 10.220.1.6
print(next(hosts_iterator)) # prints 10.220.1.7
# Or you can iterate over hosts_iterator:
for host in hosts_iterator:
print(host)
因此,基本上这可以在一行中完成(+导入和定义网络和保留地址)。如果您使用的是Python 3.3(或更新版本),则可以使用该模块。子网10.220.1.0/24
中所有主机的示例,保留的中的主机除外:
from ipaddress import IPv4Network
network = IPv4Network('10.220.1.0/24')
reserved = {'10.220.1.1', '10.220.1.2', '10.220.1.3', '10.220.1.5'}
hosts_iterator = (host for host in network.hosts() if str(host) not in reserved)
# Using hosts_iterator:
print(next(hosts_iterator)) # prints 10.220.1.4
print(next(hosts_iterator)) # prints 10.220.1.6
print(next(hosts_iterator)) # prints 10.220.1.7
# Or you can iterate over hosts_iterator:
for host in hosts_iterator:
print(host)
因此,基本上这可以在一行中完成(+网络和保留地址的导入和定义)。这不是OP要求的。这不是OP要求的。是否希望有一个循环来生成特定范围内的所有ip(10.220.1.x
)?将ip转换(并保留)为其他合法表示:整数。那么寻找一个缺口是一件小事。是的@BubbleHacker它需要匹配10.220.1的范围。x@Serge如何将IP地址转换为整数,然后再转换回来?有没有一种首选的pythony方法?除了与IPv4地址一起工作的函数的标准兼容实现应该平等地对待10.220.1.4、10.220.260、10.14418180和182190340之外,您可以为任何w.x.y.z地址自己计算:((w*256+x)*256+y)*256+zDo您希望有一个循环来生成特定范围内的所有ip吗(10.220.1.x
)?转换(并保留)将IPs转换为另一个合法的表示形式:整数。然后搜索间隙是一件小事。是的@BubbleHacker它需要匹配10.220.1的范围。x@Serge您会如何将IP地址转换成整数,然后再转换回来?除了符合标准的functi实现之外,还有首选的pythony方法吗使用IPv4地址的ons应同等对待10.220.1.4、10.220.260、10.14418180和182190340,您可以自己计算任何w.x.y.z地址:((w*256+x)*256+y)*256+zThanks,但不使用python 3…必须用于2.x。实际上,我导入了ipaddress模块并使其正常工作。因为我使用的是python 2.7.x,所以我必须将网络范围作为unicode对象而不是字符串传递…network=IPv4Network(u'10.220.1.0/24'))
谢谢,但不使用python 3…必须用于2.x。实际上,我导入了ipaddress模块并使其正常工作。因为我使用的是python 2.7.x,所以我必须将网络范围作为unicode对象而不是字符串传递…network=IPv4Network(u'10.220.1.0/24'))
谢谢,但不幸的是没有使用Python3…需要它来处理2.xThanks,但不幸的是没有使用Python3…需要它来处理2.x