class IpProcessor
Public Class Methods
merge_ips(ips)
click to toggle source
merge [“10.32.119.2”,“10.32.119.3”,“10.32.119.4”,“10.32.119.10,”10.32.119.11“] to
- “10.32.119.2-10.32.119.4”,“10.32.119.10-10.32.119.11”
# File lib/ip_processor.rb, line 26 def self.merge_ips(ips) ips = ips.split(",") if ips.is_a? String subnet = ips[0].split(".")[0..2].join(".") last_fields = ips.map{|i| i.split(".")[-1].to_i} last_fields.sort! last_field = last_fields[0] res="#{last_field}" last_fields.each do |f| if(f>last_field +1) res << ",#{f}" elsif (f == last_field+1) res << " #{f}" end last_field = f end res = res.split(",") res=res.map do |ip_list| ips=ip_list.split(" ") if ips.size == 1 subnet+"."+ip_list else "#{subnet}.#{ips[0]}-#{subnet}.#{ips[-1]}" end end end
split_ips(ip_str)
click to toggle source
split ips from “10.32.119.2 - 10.32.119.35”
# File lib/ip_processor.rb, line 4 def self.split_ips(ip_str) ips = ip_str.split("-") if ips.size == 1 return ips elsif ips.size > 2 raise "illegal format, should be ip1-ip2" elsif ips[0].split(".")[0..2] != ips[1].split(".")[0..2] raise "only support ips in same subnet, and the subnet mask is assumed to be 255.255.255" end all_ips=[] subnet = ips[0].split(".")[0..2] ipstart = ips[0].split(".")[3].to_i ipend = ips[1].split(".")[3].to_i ipstart.upto(ipend).each do |i| all_ips << (subnet + [i]).join(".") end return all_ips end