Using SQL to determine cidr value of a subnet mask

前端 未结 4 609
故里飘歌
故里飘歌 2021-02-05 21:56

I\'d like to find a way to do a SQL query that will calculate the cidr (bit representation) of a subnet mask stored in the database. So for example, I\'ve got either 255.255.25

4条回答
  •  深忆病人
    2021-02-05 22:58

    I think I have found the solution to my issue. Here is what I have done:

    select CONCAT(INET_NTOA(ip_addr),'/',32-log2((4294967296-ip_mask))) net 
    from subnets 
    order by ip_addr
    

    Basically I take my decmial mask and subtract it from the maximum decimal value. I then to a log2 on that value to get the logarithm value. Then simply subtract that from 32 (the maximum bit available).

    Hope that helps others.

    Thanks

提交回复
热议问题