Netmask to CIDR in ruby

前端 未结 7 880
灰色年华
灰色年华 2020-12-29 11:13

I\'ve been using the ip-address gem and it doesn\'t seem to have the ability to convert from a netmask of the form

255.255.255.0 

into the

相关标签:
7条回答
  • 2020-12-29 12:01

    If you don't need to use ip-address gem, you can do this with the netaddr gem

    require 'netaddr'
    
    def to_cidr_mask(dotted_mask)
      NetAddr::CIDR.create('0.0.0.0/'+dotted_mask).netmask
    end
    
    to_cidr_mask("255.224.0.0") # => "/11" 
    
    0 讨论(0)
提交回复
热议问题