sql 从IP子网掩码中获取CIDR('/')。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql 从IP子网掩码中获取CIDR('/')。相关的知识,希望对你有一定的参考价值。

/*
.SYNOPSIS
    Gets the CIDR from a IP Subnet Mask.
.DESCRIPTION
    Gets the CIDR ('/') from a IP Subnet Mask.
.PARAMETER IPSubnetMask
    Specifies the IP subnet mask.
.EXAMPLE
    SELECT dbo.ufn_CIDRFromIPMask('255.255.255.0')
.NOTES
    Created by Ioan Popovici (2018-12-11)
    Credit to Chris O'Connor
    Replace the <CM_Your_Site_Code> with your CM or custom database name.
    Run the code in SQL Server Management Studio.
.LINK
    https://clouddeveloper.space/2015/07/14/sql-inet_aton-ip-address-cidr/ (Chris o'Connor)
.LINK
    https://SCCM.Zone
.LINK
    https://SCCM.Zone/Issues
*/

/*##=============================================*/
/*## QUERY BODY                                  */
/*##=============================================*/
/* #region QueryBody */

USE [<CM_Your_Site_Code/Custom_Function_Database>]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO

IF EXISTS (
    SELECT [OBJECT_ID]
    FROM   [SYS].[OBJECTS]
    WHERE  NAME = 'ufn_CIDRFromIPMask'
)
    DROP FUNCTION [dbo].[ufn_CIDRFromIPMask];
GO

CREATE FUNCTION [dbo].[ufn_CIDRFromIPMask] (
    @IPSubnetMask VARCHAR(15)
)
RETURNS VARCHAR(4)
AS
    BEGIN

        /* Variable declaration */
        DECLARE @IPSubnetMaskToInteger AS BIGINT;
        DECLARE @IPDefaultSubnetMaskToInteger AS BIGINT;
        DECLARE @MaskCalc AS BIGINT;
        DECLARE @LogarithmCacl AS INT;
        DECLARE @Result AS VARCHAR(4);

        /* Convert IP subnet mask to integer */
        SET @IPSubnetMaskToInteger        = (
            CONVERT(BIGINT, PARSENAME(@IPSubnetMask,1)) +
            CONVERT(BIGINT, PARSENAME(@IPSubnetMask,2)) * 256 +
            CONVERT(BIGINT, PARSENAME(@IPSubnetMask,3)) * 65536 +
            CONVERT(BIGINT, PARSENAME(@IPSubnetMask,4)) * 16777216
        );

        /* Convert default IP subnet mask (255.255.255.255) to integer */
        SET @IPDefaultSubnetMaskToInteger = (
            CONVERT(BIGINT, 255) +
            CONVERT(BIGINT, 255) * 256 +
            CONVERT(BIGINT, 255) * 65536 +
            CONVERT(BIGINT, 255) * 16777216
        );

        /* Calculate mask */
        SET @MaskCalc                     = (@IPDefaultSubnetMaskToInteger - @IPSubnetMaskToInteger + 1);
        SET @LogarithmCacl                = (32 - LOG (@MaskCalc, 2));

        /* Calculate result */
        SET @Result = '/' + CAST(@LogarithmCacl AS VARCHAR(5));

        /* Return result */
        RETURN  @Result;
    END;

/* #endregion */
/*##=============================================*/
/*## END QUERY BODY                              */
/*##=============================================*/

以上是关于sql 从IP子网掩码中获取CIDR('/')。的主要内容,如果未能解决你的问题,请参考以下文章

网络层——可变长子网掩码(VLSM)和无类域间路由(CIDR)技术

VLSM和CIDR-路由交换原理3-HCNA笔记

VLSM和CIDR-路由交换原理3-HCNA笔记

IP地址掩码以及CIDR掩码理解计算问题

如何使用python获取ip和子网掩码地址[关闭]

给定 IP 地址和子网掩码,如何计算 CIDR?