代碼示例:
import random
import struct
import socket
RANDOM_IP_POOL = ['192.168.10.222/0']
def __get_random_ip():
str_ip = RANDOM_IP_POOL[random.randint(0, len(RANDOM_IP_POOL) - 1)]
str_ip_addr = str_ip.split('/')[0]
str_ip_mask = str_ip.split('/')[1]
ip_addr = struct.unpack('>I', socket.inet_aton(str_ip_addr))[0]
mask = 0x0
for i in range(31, 31 - int(str_ip_mask), -1):
mask = mask | (1 << i)
ip_addr_min = ip_addr & (mask & 0xffffffff)
ip_addr_max = ip_addr | (~mask & 0xffffffff)
return socket.inet_ntoa(struct.pack('>I', random.randint(ip_addr_min, ip_addr_max)))
if __name__ == "__main__":
print(__get_random_ip())