保留的IP地址Python

m528fe3b  于 2022-11-26  发布在  Python
关注(0)|答案(1)|浏览(172)

我试图检查用户是否试图分配一个保留地址。目前我们正在保留前5个和后5个地址以备将来使用。我这样做的方式atm真的很慢,在ipv6交换机的情况下不会有效地工作。有没有其他方法来优化检查?

def verify_ip_subnet(ip_address, subnet_address):
    new_subnet = subnet_address.split('/')[1].strip()
    if int(new_subnet) == 16:
        new_vpn_range = []
        new_vpn_range.append(subnet_address.split('/')[0].strip())
        new_count = 0
        for address in ipaddress.ip_network(subnet_address).hosts():
            if new_count == 0 or new_count == 1 or new_count == 2 or new_count == 3 or new_count == 4 or new_count == 65531 or new_count == 65532 or new_count == 65533:
                new_vpn_range.append(str(address))
                new_count += 1
        if ip_address in new_vpn_range:
            return False
    if int(new_subnet) == 24:
        new_vpn_range = []
        new_vpn_range.append(subnet_address.split('/')[0].strip())
        new_count = 0
        for address in ipaddress.ip_network(subnet_address).hosts():
            if new_count == 0 or new_count == 1 or new_count == 2 or new_count == 3 or new_count == 4 or new_count == 251 or new_count == 252 or new_count == 253:
                new_vpn_range.append(str(address))
                new_count += 1
            if ip_address in new_vpn_range:
                return False

以下是导入的示例:

ip_address = '10.8.0.5'
subnet_address = '10.8.0.0/16'

另一种方法是使用num_addresses在这种情况下我可以检查前5个保留的地址,但是如果我试图检查最后5个地址,索引就会超出范围。下面是一个我如何检查它们的例子:

ip_check = ipaddress.ip_network(subnet_address)
for i in range(6):
    if str(ip_check[i]) == ip_address:
        return False

last_address = ip_check.num_addresses
print(last_address)
print(ip_check[last_address])
rxztt3cl

rxztt3cl1#

如果要了解IP是否保留用于其他一般用途,可以使用具有布尔属性(is_reserved)的ipaddress.ip_address来了解IP是否保留用于IPv4和IPv6:

# Changing your variable name to avoid confusion with module function
>>> from ipaddress import ip_address, ip_network
>>> ip = ip_address('10.8.0.5')
>>> ip.is_reserved
True
>>> ip_v6 = ip_address('10.8.0.5')
>>> ip_v6.is_reserved
True
>>> subnet = ip_network('10.8.0.0/16')
>>> ip in subnet
True

还有许多其他属性,如is_privateis_globalis_loopback ...
当你想指定前五个和后五个时,你可以使用你所使用的相同代码,但是错误在最后一行,因为索引从0开始。
我修改为使用ip_address函数:

>>> from ipaddress import ip_address, ip_network
>>> # Range of reserved IPs from the beginning and end of the IP network
>>> RESERVED_RANGE = 6
>>> ip = ip_address('10.8.0.4')
>>> subnet = ip_network('10.8.0.0/16')
>>> 
>>> for i in range(RESERVED_RANGE):
>>>     print(i, subnet[i], end=", ")
>>>     # To only have on iteration for first and last 
>>>     # I'm using "~" a bitwise operator that returns the complement of i
>>>     # You can see on the printed output below how it works
>>>     print(~i, subnet[~i])
>>>     if ip == subnet[i] or ip == subnet[~i]:        
>>>         break
>>> 
>>> num_address = subnet.num_addresses
>>> print("Number of address", num_address)
>>> print("Last IP in subnet", subnet[num_address - 1])
0 10.8.0.0, -1 10.8.255.255
1 10.8.0.1, -2 10.8.255.254
2 10.8.0.2, -3 10.8.255.253
3 10.8.0.3, -4 10.8.255.252
4 10.8.0.4, -5 10.8.255.251
Number of address 65536
Last IP in subnet 10.8.255.255

相关问题