工作中需要将IPv4和IPv6的地址和掩码转换为地址范围,写了下面两端python代码来自动计算。代码中没有注释,有疑问请留言。
下面是IPv4地址转换部分,输入比如”192.168.111.253/28, 返回字典['192.168.111.240', '192.168.111.255']
# coding: utf-8
class Ipv4_range:
def part(self,part_ip,part_mask):
part_ip = int(part_ip)
part_mask = int(part_mask)
delta = 255-part_mask
ip_min = part_ip & part_mask
ip_max = ip_min + delta
return ip_min,ip_max
def ips(self,ip_mask):
sep = ip_mask.split('/')
ip = sep[0]
mask = self.msk_trans(sep[1])
part_ips = ip.split('.')
part_masks = mask.split('.')
ip_min = ''
ip_max = ''
for i in range(0,4):
part_min, part_max = self.part(part_ips[i],part_masks[i])
ip_min += str(part_min) + '.'
ip_max +