rocky_tools 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d5663aec8c78704fbe29cb36f2d3fcc85e16ef51
4
+ data.tar.gz: d760962a5c181d3e11230f995309679b4fcc5eb4
5
+ SHA512:
6
+ metadata.gz: 1be862aedabc3bf7729270d5599c12ae377b95b498d562246c25f0bf8b60012a4c6c53f7e6828f0ffabf7ed71084e82b00843b96e566161f22ed5d7fa981f436
7
+ data.tar.gz: b02b5407cfeafa20174e195bf65574d6009091a77bcbf29b22a627335786421077254d3d923c70700393e3117f9c761f914ed975b9b0c42df3d60e31217a6cac
@@ -0,0 +1,52 @@
1
+ class IpProcessor
2
+
3
+ #split ips from "10.32.119.2 - 10.32.119.35"
4
+ def self.split_ips(ip_str)
5
+ ips = ip_str.split("-")
6
+ if ips.size == 1
7
+ return ips
8
+ elsif ips.size > 2
9
+ raise "illegal format, should be ip1-ip2"
10
+ elsif ips[0].split(".")[0..2] != ips[1].split(".")[0..2]
11
+ raise "only support ips in same subnet, and the subnet mask is assumed to be 255.255.255"
12
+ end
13
+
14
+ all_ips=[]
15
+ subnet = ips[0].split(".")[0..2]
16
+ ipstart = ips[0].split(".")[3].to_i
17
+ ipend = ips[1].split(".")[3].to_i
18
+ ipstart.upto(ipend).each do |i|
19
+ all_ips << (subnet + [i]).join(".")
20
+ end
21
+ return all_ips
22
+ end
23
+
24
+ #merge ["10.32.119.2","10.32.119.3","10.32.119.4","10.32.119.10,"10.32.119.11"] to
25
+ #["10.32.119.2-10.32.119.4","10.32.119.10-10.32.119.11"]
26
+ def self.merge_ips(ips)
27
+ ips = ips.split(",") if ips.is_a? String
28
+
29
+ subnet = ips[0].split(".")[0..2].join(".")
30
+ last_fields = ips.map{|i| i.split(".")[-1].to_i}
31
+ last_fields.sort!
32
+ last_field = last_fields[0]
33
+ res="#{last_field}"
34
+ last_fields.each do |f|
35
+ if(f>last_field +1)
36
+ res << ",#{f}"
37
+ elsif (f == last_field+1)
38
+ res << " #{f}"
39
+ end
40
+ last_field = f
41
+ end
42
+ res = res.split(",")
43
+ res=res.map do |ip_list|
44
+ ips=ip_list.split(" ")
45
+ if ips.size == 1
46
+ subnet+"."+ip_list
47
+ else
48
+ "#{subnet}.#{ips[0]}-#{subnet}.#{ips[-1]}"
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,6 @@
1
+ require 'ip_processor'
2
+ class RockyTools
3
+ def self.hi
4
+ puts "Hi, these are tools from Rocky"
5
+ end
6
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rocky_tools
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - rockydd
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-31 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Rocky's Tools
14
+ email: rockydd@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/ip_processor.rb
20
+ - lib/rocky_tools.rb
21
+ homepage: http://rubygems.org/gems/rocky_tools
22
+ licenses:
23
+ - MIT
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 2.2.2
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: Hello Rocky!
45
+ test_files: []