我是靠谱客的博主 迷人纸飞机,最近开发中收集的这篇文章主要介绍通过IP地址和掩码计算地址范围(ipv4/ipv6),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

工作中需要将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 += str(part_max) + '.'
        ip_min = ip_min[:-1]
        ip_max = ip_max[:-1]
        return ([ip_min,ip_max])

    def msk_trans(self,digit):
        add = 32 - int(digit)
        msk_str_b = '1' * int(digit) + '0' * add

        msk1 = msk_str_b[0:8]
        msk2 = msk_str_b[8:16]
        msk3 = msk_str_b[16:24]
        msk4 = msk_str_b[24:32]

        msk1_deci = int(msk1, 2)
        msk2_deci = int(msk2, 2)
        msk3_deci = int(msk3, 2)
        msk4_deci = int(msk4, 2)

        result = str(msk1_deci) + '.' + str(msk2_deci) + '.' + str(msk3_deci) + '.' + str(msk4_deci)
        return result

if __name__ == '__main__':
    ips = Ipv4_range()
    a= ips.ips('192.168.111.253/28')
    print(a)

 下面是IPv6地址转换部分,IPv6比IPv4多一步,因为IPv6地址可能会简写,用两个冒号代替几组零,所以增加了一步转换,首先把“::” 展开为“0”。

# coding: utf-8

class Ipv6_range:

    def ipv6_add0(self,ip):
    # ip = '2409:8000::4808:1741'
        if '::' in ip:
            # print(ip.split(':'))
            if ip[-2:] == '::':
                numb = len(ip.split(':')) - 2
                add = ':0' * (8 - numb)
            else:
                numb = len(ip.split(':')) - 1
                add = ':0' * (8 - numb) + ':'

            ip_add_0 = ip.replace('::', add)
            ip = ip_add_0
        return ip


    def msk_trans(self,digit):
        add = 128 - int(digit)
        msk_str_b = '1' * int(digit) + '0' * add
        msk1 = msk_str_b[0:16]
        msk2 = msk_str_b[16:32]
        msk3 = msk_str_b[32:48]
        msk4 = msk_str_b[48:64]
        msk5 = msk_str_b[64:80]
        msk6 = msk_str_b[80:96]
        msk7 = msk_str_b[96:112]
        msk8 = msk_str_b[112:128]

        msk1_deci = int(msk1, 2)
        msk2_deci = int(msk2, 2)
        msk3_deci = int(msk3, 2)
        msk4_deci = int(msk4, 2)
        msk5_deci = int(msk5, 2)
        msk6_deci = int(msk6, 2)
        msk7_deci = int(msk7, 2)
        msk8_deci = int(msk8, 2)

        result = str(msk1_deci) + '.' + str(msk2_deci) + '.' + str(msk3_deci) + '.' + str(msk4_deci) + '.' +
        str(msk5_deci) + '.' + str(msk6_deci) + '.' + str(msk7_deci) + '.' + str(msk8_deci)

        return result

    def part(self,part_ip,part_mask):
        part_ip = int(part_ip,16)
        part_mask = int(part_mask)
        delta = 65535-part_mask

        ip_min = part_ip & part_mask
        ip_max = ip_min + delta

        s_ip_min = str(hex(ip_min)).removeprefix('0x')
        s_ip_max = str(hex(ip_max)).removeprefix('0x')

        return s_ip_min,s_ip_max

    def ips(self,ip_mask):
        sep = ip_mask.split('/')
        ip = sep[0]
        ip = self.ipv6_add0(ip)
        mask = self.msk_trans(sep[1])
        # print(ip)
        # print(mask)
        part_ips = ip.split(':')
        part_masks = mask.split('.')
        ip_min = ''
        ip_max = ''
        for i in range(0,8):
            part_min, part_max = self.part(part_ips[i],part_masks[i])
            ip_min += str(part_min) + ':'
            ip_max += str(part_max) + ':'
        ip_min = ip_min[:-1]
        ip_max = ip_max[:-1]
        return ([ip_min,ip_max])



if __name__ == '__main__':
    ip_range = Ipv6_range()
    ip = 'fe80:8000:480a::1741/127'
    a = ip_range.ips(ip)
    print(a)

 

最后

以上就是迷人纸飞机为你收集整理的通过IP地址和掩码计算地址范围(ipv4/ipv6)的全部内容,希望文章能够帮你解决通过IP地址和掩码计算地址范围(ipv4/ipv6)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(64)

评论列表共有 0 条评论

立即
投稿
返回
顶部