Beispiel #1
0
    def get_macip_by_id(macip_id: int):
        """
        通过id获取mac ip

        :param macip_id: mac ip id
        :return:
            MacIP() # success
            None    #不存在

        :raise NetworkError
        """
        if not isinstance(macip_id, int) or macip_id < 0:
            raise NetworkError(msg='MacIP ID参数有误')

        try:
            return MacIP.objects.filter(id=macip_id).first()
        except Exception as e:
            raise NetworkError(msg=f'查询MacIP时错误,{str(e)}')
Beispiel #2
0
    def get_vlan_by_id(vlan_id: int):
        """
        通过id获取镜像元数据模型对象
        :param vlan_id: 镜像id
        :return:
            Vlan() # success
            None    #不存在

        :raise NetworkError
        """
        if not isinstance(vlan_id, int) or vlan_id < 0:
            raise NetworkError(msg='子网ID参数有误')

        try:
            return Vlan.objects.select_related('group').filter(
                id=vlan_id).first()
        except Exception as e:
            raise NetworkError(msg=f'查询子网时错误,{str(e)}')
Beispiel #3
0
    def get_macip_by_ipv4(ipv4: str):
        """
        通过ipv4获取mac ip

        :param ipv4: ip地址
        :return:
            MacIP() # success
            None    #不存在

        :raise NetworkError
        """
        if not ipv4 or not isinstance(ipv4, str):
            raise NetworkError(msg='ipv4参数有误')

        try:
            return MacIP.objects.filter(
                ipv4=ipv4).select_related('vlan').first()
        except Exception as e:
            raise NetworkError(msg=f'查询MacIP时错误,{str(e)}')
Beispiel #4
0
 def get_macips_by_vlan(vlan):
     """
     获得vlan对应的所有macip记录
     :param vlan:
     :return: 直接返回查询结果
     """
     try:
         macips = MacIP.objects.filter(vlan=vlan)
     except Exception as error:
         raise NetworkError(msg='读取macips失败。' + str(error))
     return macips
Beispiel #5
0
    def generate_subips(self, vlan_id, from_ip, to_ip, write_database=False):
        '''
        生成子网ip
        :param vlan_id:
        :param from_ip: 开始ip
        :param to_ip: 结束ip
        :param write_database: True 生成并导入到数据库 False 生成不导入数据库
        :return:
        '''
        reg = r"^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"
        if not (re.match(reg, from_ip) and re.match(reg, to_ip)):
            raise NetworkError(msg='输入的ip地址错误')
        ip_int = [[*map(int, ip.split('.'))] for ip in (from_ip, to_ip)]
        ip_hex = [
            ip[0] << 24 | ip[1] << 16 | ip[2] << 8 | ip[3] for ip in ip_int
        ]
        if ip_hex[0] > ip_hex[1]:
            raise NetworkError(msg='输入的ip地址错误')

        subips = [
            f'{ip >> 24}.{(ip & 0x00ff0000) >> 16}.{(ip & 0x0000ff00) >> 8}.{ip & 0x000000ff}'
            for ip in range(ip_hex[0], ip_hex[1] + 1) if ip & 0xff
        ]
        submacs = [
            'C8:00:' + ':'.join(
                map(lambda x: x[2:].upper().rjust(2, '0'),
                    map(lambda x: hex(int(x)), ip.split('.'))))
            for ip in subips
        ]
        if write_database:
            with transaction.atomic():
                for subip, submac in zip(subips, submacs):
                    try:
                        MacIP.objects.create(vlan_id=vlan_id,
                                             ipv4=subip,
                                             mac=submac)
                    except Exception as error:
                        raise NetworkError(msg='ip写入数据库失败,部分ip数据库中已有')

        return [*zip(subips, submacs)]
Beispiel #6
0
    def generate_subips(vlan_id, from_ip, to_ip, write_database=False):
        """
        生成子网ip
        :param vlan_id:
        :param from_ip: 开始ip
        :param to_ip: 结束ip
        :param write_database: True 生成并导入到数据库 False 生成不导入数据库
        :return:
        """
        try:
            ipv4_from = IPv4Address(from_ip)
            ipv4_to = IPv4Address(to_ip)
        except AddressValueError as e:
            raise NetworkError(msg=f'输入的ip地址无效, {e}')

        int_from = int(ipv4_from)
        int_to = int(ipv4_to)
        if int_from > int_to:
            raise NetworkError(msg='请检查输入的ip地址的范围,起始ip不能大于结束ip地址')

        l_ip_mac = []
        for ip in range(int_from, int_to + 1):
            if ip & 0xff:
                ip_obj = IPv4Address(ip)
                p = ip_obj.packed
                mac = f'C8:00:{p[0]:02X}:{p[1]:02X}:{p[2]:02X}:{p[3]:02X}'
                l_ip_mac.append((str(ip_obj), mac))

        if write_database:
            with transaction.atomic():
                for subip, submac in l_ip_mac:
                    try:
                        MacIP.objects.create(vlan_id=vlan_id,
                                             ipv4=subip,
                                             mac=submac)
                    except Exception as error:
                        raise NetworkError(msg='ip写入数据库失败,部分ip数据库中已有')

        return l_ip_mac