Python で ipaddress 生成#
create_ipaddrss.py
import ipaddress
import itertools as it
import re
class BaseIPv6Network(ipaddress.IPv6Network):
def __add__(self, offset):
new_base_addr = int(self.network_address) + offset
return self.__class__((new_base_addr, self.prefixlen))
def size(self):
# Return network size.
return 1 << (self.max_prefixlen - self.prefixlen)
def increment_ipv6addr(ipv6net, self_addr, ipv6_range):
ipv6_list = []
network = BaseIPv6Network(ipv6net)
network_addrs = (network + i * network.size() for i in it.count())
for _ in range(ipv6_range):
ipv6_net = str(next(network_addrs))
ipv6_mask = re.search("/\d+",ipv6_net ).group()
last_addr = str(self_addr) + ipv6_mask
ipv6addr = ipv6_net.replace(ipv6_mask,last_addr)
ipv6_list.append(ipv6addr)
return ipv6_list
# 確認用
# test = increment_ipv6addr("192:168:1:1::/64",1,10)
# print(test)
def create_ipv4addres(ipv4addr, mask, ipv4_range, ipv4_step):
ipv4_list = []
ipv4addr = ipaddress.ip_address(str(ipv4addr))
mask = str(mask)
for i in range(ipv4_range):
ipv4 = f"{ipv4addr} " + mask
ipv4_list.append(ipv4)
ipv4addr += ipv4_step
return ipv4_list
# 確認用
# ipv4list = create_ipv4addres("192.168.1.1", "255.255.255.252", 10, 4)
# print(ipv4list)