net-ip 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -18,7 +18,25 @@ Or install it yourself as:
18
18
 
19
19
  ## Usage
20
20
 
21
- TODO: Write usage instructions here
21
+ ````ruby
22
+ require 'net/ip'
23
+
24
+ Net::IP::Route.flush(:cache)
25
+
26
+ Net::IP::Route.each do |rt|
27
+ p rt
28
+ end
29
+
30
+ Net::IP::Route.find_gateways("eth0").each do |gw|
31
+ puts gw.via
32
+ end
33
+
34
+ gws = ["192.168.0.1", "192.168.0.2"].collect do |ip|
35
+ Net::IP::Route.new(:via => ip, :dev => "eth0", :weight => 1)
36
+ end
37
+
38
+ Net::IP::Route.update_gateways(gws)
39
+ ````
22
40
 
23
41
  ## Contributing
24
42
 
data/lib/net/ip/route.rb CHANGED
@@ -1,85 +1,91 @@
1
1
  module Net
2
2
  module IP
3
+ # Class for working with routing table entries.
3
4
  class Route
4
5
  attr_accessor :type, :prefix, :dev, :scope, :metric,
5
6
  :proto, :src, :via, :weight, :table, :error
6
7
 
7
- def to_s
8
- str = ""
9
- str << type << " " if type != "unicast"
10
- str << prefix << " "
11
-
12
- str << "via #{via} " if via
13
-
14
- str << "dev #{dev} " if dev
15
-
16
- str << " table #{table} " if table
17
- str << " proto #{proto} " if proto
18
- str << " scope #{scope} " if scope
19
-
20
- str << " src #{src} " if src
21
- str << " metric #{metric} " if metric
22
-
23
- str << " error #{error}" if error
8
+ extend Enumerable
9
+
10
+ # Create a new route object
11
+ # @example Create a default route
12
+ # Net::IP::Route.new(:prefix => 'default', :via => '192.168.0.1')
13
+ # @example Create a normal route
14
+ # Net::IP::Route.new(:prefix => '10.0.0.0/8', :dev => 'eth0')
15
+ # @note This does NOT add the entry to the routing table. See {add_route} for creating new routes in the routing table.
16
+ # @param params {Hash}
17
+ def initialize(params = {})
18
+ params.each do |k,v|
19
+ send("#{k}=", v)
20
+ end
21
+ end
24
22
 
25
- str
23
+ # Enumerate all routes
24
+ # @yield {Route}
25
+ # @return {void}
26
+ def self.each(&block)
27
+ RouteParser.parse(`ip route`).each {|r| yield(new(r))}
26
28
  end
27
29
 
30
+ # Get a list of all routes
31
+ # @return {Array<Route>}
28
32
  def self.all
29
- parse(`ip route show table 0`)
33
+ RouteParser.parse(`ip route`).collect {|r| new(r)}
30
34
  end
31
35
 
32
- private
33
-
34
- def self.parse_line(line)
35
- route = new
36
-
37
- if line =~ /^(unicast|unreachable|blackhole|prohibit|local|broadcast|throw|nat|via|anycast|multicast)\s+/
38
- route.type = $1
39
- line = line[$1.length..-1]
40
- else
41
- route.type = "unicast"
42
- end
43
-
44
- route.prefix = line.split.first
45
- route.dev = $1 if line =~ /\s+dev\s+([^\s]+)/
46
- route.scope = $1 if line =~ /\s+scope\s+([^\s]+)/
47
- route.metric = $1 if line =~ /\s+metric\s+([^\s]+)/
48
- route.proto = $1 if line =~ /\s+proto\s+([^\s]+)/
49
- route.src = $1 if line =~ /\s+src\s+([^\s]+)/
50
- route.via = $1 if line =~ /\s+via\s+([^\s]+)/
51
- route.weight = $1 if line =~ /\s+weight\s+([^\s]+)/
52
- route.table = $1 if line =~ /\s+table\s+([^\s]+)/
53
- route.error = $1 if line =~ /\s+error\s+([^\s]+)/
36
+ # Get a list of all default gateway routes
37
+ # @return {Array<Route>}
38
+ def self.find_gateways
39
+ find_all {|r| r.prefix == "default"}
40
+ end
54
41
 
55
- route
42
+ # Update the list of default gateways
43
+ # @example Change the default gateway to 192.168.0.1
44
+ # gateway = Net::IP::Route.new(:prefix => 'default', :via => '192.168.0.1')
45
+ # Net::IP::Route.update_gateways([gateway])
46
+ # @param gateways {Array<Route>} List of default gateways to use.
47
+ # @return {void}
48
+ def self.update_gateways(gateways)
49
+ params = gateways.collect {|gateway| "nexthop " + gateway.build_param_string}
50
+ result = `ip route replace default #{params.join(" ")}`
51
+ raise result unless $?.success?
56
52
  end
57
53
 
58
- def self.parse_data(data, &block)
59
- in_default = false
60
- data.each_line do |line|
61
- if in_default == true
62
- if line.start_with? "\t"
63
- yield(parse_line(line.strip.gsub("nexthop", "default")))
64
- else
65
- in_default = false
66
- end
67
- elsif line.strip == "default"
68
- in_default = true
69
- end
54
+ # Add a route to the routing table
55
+ # @example Create a route to the 10.0.0.0/8 network
56
+ # route = Net::IP::Route.new(:prefix => '10.0.0.0/8', :dev => 'eth0')
57
+ # Net::IP::Route.add_route(route)
58
+ # @param route {Route} Route to add to the table.
59
+ # @return {void}
60
+ def self.add_route(route)
61
+ result = `ip route add #{route.build_param_string}`
62
+ raise result unless $?.success?
63
+ end
70
64
 
71
- unless in_default
72
- yield(parse_line(line.strip))
73
- end
74
- end
65
+ # Flush the routing table based on a selector
66
+ # @example Flush the routing table cache
67
+ # Net::IP::Route.flush(:cache)
68
+ # @param selector {String} The selector string.
69
+ # @return {void}
70
+ def self.flush(selector)
71
+ result = `ip route flush #{selector}`
72
+ raise result unless $?.success?
75
73
  end
76
74
 
77
- def self.parse(data)
78
- list = []
79
- parse_data(data) do |route|
80
- list << route
81
- end
82
- list
75
+ private
76
+
77
+ def build_param_string
78
+ str = ""
79
+ str << "via #{via} " if via
80
+ str << "dev #{dev} " if dev
81
+ str << "weight #{weight}" if weight
82
+ str << " table #{table} " if table
83
+ str << " proto #{proto} " if proto
84
+ str << " scope #{scope} " if scope
85
+ str << " src #{src} " if src
86
+ str << " metric #{metric} " if metric
87
+ str << " error #{error}" if error
88
+ str
83
89
  end
84
90
  end
85
91
  end
@@ -1,5 +1,5 @@
1
1
  module Net
2
2
  module IP
3
- VERSION = "0.0.2"
3
+ VERSION = "0.0.3"
4
4
  end
5
5
  end
data/lib/net/ip.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require "net/ip/version"
2
+ require "net/ip/route_parser"
2
3
  require "net/ip/route"
3
4
 
4
5
  module Net
data/net-ip.gemspec CHANGED
@@ -9,7 +9,7 @@ Gem::Specification.new do |spec|
9
9
  spec.authors = ["Joshua Bussdieker"]
10
10
  spec.email = ["jbussdieker@gmail.com"]
11
11
  spec.summary = %q{Tools for working with IP routes}
12
- spec.homepage = ""
12
+ spec.homepage = "http://github.com/jbussdieker/ruby-net-ip"
13
13
  spec.license = "MIT"
14
14
 
15
15
  spec.files = `git ls-files`.split($/)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: net-ip
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-08-08 00:00:00.000000000 Z
12
+ date: 2013-08-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -75,7 +75,7 @@ files:
75
75
  - lib/net/ip/route.rb
76
76
  - lib/net/ip/version.rb
77
77
  - net-ip.gemspec
78
- homepage: ''
78
+ homepage: http://github.com/jbussdieker/ruby-net-ip
79
79
  licenses:
80
80
  - MIT
81
81
  post_install_message:
@@ -90,7 +90,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
90
90
  version: '0'
91
91
  segments:
92
92
  - 0
93
- hash: 898382037184904844
93
+ hash: 2052506917589996489
94
94
  required_rubygems_version: !ruby/object:Gem::Requirement
95
95
  none: false
96
96
  requirements:
@@ -99,7 +99,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
99
99
  version: '0'
100
100
  segments:
101
101
  - 0
102
- hash: 898382037184904844
102
+ hash: 2052506917589996489
103
103
  requirements: []
104
104
  rubyforge_project:
105
105
  rubygems_version: 1.8.25