Friday, 15 February 2013

ip - Netmask to CIDR in ruby -


I am using an IP address address and it does not appear that the netmask form

  255.255.255.0  

In CIDR form

  / 24  

Does anyone have any ideas , Which is to be changed later in the latter?

quick and dirty paths

  require 'ipaddr' Ipadder NUU ("255.255.255.0") is called. To_i.to_s (2) .count ("1")  

This should be the right job, I can not find it, so I just count "1". >

If you are going to use the function in many places and don 'port in the mindpatching, it can help:

  IPAddr.class_eval def to_cidr "/" + self. To_i.to_s (2) .Count ("1") End End  

Then you get

  IPAddr.new ('255.255.255.0') . To_cidr # = & gt; "/ 24"  

No comments:

Post a Comment