ruby - Count IP addresses -
i stored following ips in array:
10.2.3.1 10.2.3.5 10.2.3.10 - 10.2.3.15
i'm trying count total number of ip addresses. total number of ip addresses should 8. when iterate through array, total of 3 counts. need way count third item:
10.2.3.10 - 10.2.3.15
are there ip address counters?
if need convert ip range, you'll need function converts ipv4 value integer, math on those:
require 'ipaddr' def ip(string) ipaddr.new(string).to_i end def parse_ip(string) string.split(/\s+\-\s+/).collect { |v| ip(v) } end def ip_range(string) ips = parse_ip(string) ips.last - ips.first + 1 end ip_range("10.2.3.10 - 10.2.3.15") # => 6
that should it.
Comments
Post a Comment