net-ip 0.0.3 → 0.0.4
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/.travis.yml +7 -0
- data/README.md +9 -27
- data/Rakefile +5 -0
- data/lib/net/ip/route_parser.rb +57 -0
- data/lib/net/ip/version.rb +1 -1
- data/spec/net/ip/route_parser_spec.rb +100 -0
- data/spec/net/ip/route_spec.rb +7 -0
- data/spec/net/ip/sample1 +3 -0
- data/spec/net/ip/sample2 +6 -0
- data/spec/net/ip/sample3 +4 -0
- data/spec/net/ip/sample4 +16 -0
- metadata +84 -64
data/.travis.yml
ADDED
data/README.md
CHANGED
@@ -1,20 +1,10 @@
|
|
1
|
-
# Net::
|
1
|
+
# Net::IP
|
2
2
|
|
3
|
-
|
3
|
+
[](https://travis-ci.org/jbussdieker/ruby-net-ip)
|
4
|
+
[](https://codeclimate.com/github/jbussdieker/ruby-net-ip)
|
5
|
+
[](http://badge.fury.io/rb/net-ip)
|
4
6
|
|
5
|
-
|
6
|
-
|
7
|
-
Add this line to your application's Gemfile:
|
8
|
-
|
9
|
-
gem 'net-ip'
|
10
|
-
|
11
|
-
And then execute:
|
12
|
-
|
13
|
-
$ bundle
|
14
|
-
|
15
|
-
Or install it yourself as:
|
16
|
-
|
17
|
-
$ gem install net-ip
|
7
|
+
Tools for working with IP routes
|
18
8
|
|
19
9
|
## Usage
|
20
10
|
|
@@ -23,12 +13,12 @@ require 'net/ip'
|
|
23
13
|
|
24
14
|
Net::IP::Route.flush(:cache)
|
25
15
|
|
26
|
-
Net::IP::Route.each do |
|
27
|
-
|
16
|
+
Net::IP::Route.each do |route|
|
17
|
+
puts route
|
28
18
|
end
|
29
19
|
|
30
|
-
Net::IP::Route.find_gateways("eth0").each do |
|
31
|
-
puts
|
20
|
+
Net::IP::Route.find_gateways("eth0").each do |gateway|
|
21
|
+
puts gateway.via
|
32
22
|
end
|
33
23
|
|
34
24
|
gws = ["192.168.0.1", "192.168.0.2"].collect do |ip|
|
@@ -37,11 +27,3 @@ end
|
|
37
27
|
|
38
28
|
Net::IP::Route.update_gateways(gws)
|
39
29
|
````
|
40
|
-
|
41
|
-
## Contributing
|
42
|
-
|
43
|
-
1. Fork it
|
44
|
-
2. Create your feature branch (`git checkout -b my-new-feature`)
|
45
|
-
3. Commit your changes (`git commit -am 'Add some feature'`)
|
46
|
-
4. Push to the branch (`git push origin my-new-feature`)
|
47
|
-
5. Create new Pull Request
|
data/Rakefile
CHANGED
@@ -0,0 +1,57 @@
|
|
1
|
+
module Net
|
2
|
+
module IP
|
3
|
+
# Parses routing table entries
|
4
|
+
class RouteParser
|
5
|
+
# Parse a routing table entry into a hash
|
6
|
+
#
|
7
|
+
# @param line {String}
|
8
|
+
# @return {Hash}
|
9
|
+
def self.parse_line(line)
|
10
|
+
params = {}
|
11
|
+
|
12
|
+
if line =~ /^(unicast|unreachable|blackhole|prohibit|local|broadcast|throw|nat|via|anycast|multicast)\s+/
|
13
|
+
params[:type] = $1
|
14
|
+
line = line[$1.length..-1]
|
15
|
+
end
|
16
|
+
|
17
|
+
params[:prefix] = line.split.first
|
18
|
+
params[:dev] = $1 if line =~ /\s+dev\s+([^\s]+)/
|
19
|
+
params[:scope] = $1 if line =~ /\s+scope\s+([^\s]+)/
|
20
|
+
params[:metric] = $1 if line =~ /\s+metric\s+([^\s]+)/
|
21
|
+
params[:proto] = $1 if line =~ /\s+proto\s+([^\s]+)/
|
22
|
+
params[:src] = $1 if line =~ /\s+src\s+([^\s]+)/
|
23
|
+
params[:via] = $1 if line =~ /\s+via\s+([^\s]+)/
|
24
|
+
params[:weight] = $1 if line =~ /\s+weight\s+([^\s]+)/
|
25
|
+
params[:table] = $1 if line =~ /\s+table\s+([^\s]+)/
|
26
|
+
params[:error] = $1 if line =~ /\s+error\s+([^\s]+)/
|
27
|
+
|
28
|
+
params
|
29
|
+
end
|
30
|
+
|
31
|
+
# Parse the output of ip route into an array of hashes
|
32
|
+
#
|
33
|
+
# @param data {String}
|
34
|
+
# @return {Array}
|
35
|
+
def self.parse(data)
|
36
|
+
list = []
|
37
|
+
in_default = false
|
38
|
+
data.each_line do |line|
|
39
|
+
if in_default == true
|
40
|
+
if line.start_with? "\t"
|
41
|
+
list << parse_line(line.strip.gsub("nexthop", "default"))
|
42
|
+
else
|
43
|
+
in_default = false
|
44
|
+
end
|
45
|
+
elsif line.strip == "default"
|
46
|
+
in_default = true
|
47
|
+
end
|
48
|
+
|
49
|
+
unless in_default
|
50
|
+
list << parse_line(line.strip)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
list
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
data/lib/net/ip/version.rb
CHANGED
@@ -0,0 +1,100 @@
|
|
1
|
+
require 'net/ip/route_parser'
|
2
|
+
|
3
|
+
describe Net::IP::RouteParser do
|
4
|
+
context "line parser" do
|
5
|
+
def test_parse_line(line, field, expect)
|
6
|
+
h = Net::IP::RouteParser.parse_line(line)
|
7
|
+
h[field].should eql(expect)
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should parse prefix" do
|
11
|
+
test_parse_line("1.1.0.0/16 dev eth0 scope link metric 1000", :prefix, "1.1.0.0/16")
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should parse dev" do
|
15
|
+
test_parse_line("1.1.0.0/16 dev eth0 scope link metric 1000", :dev, "eth0")
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should parse scope" do
|
19
|
+
test_parse_line("1.1.0.0/16 dev eth0 scope link metric 1000", :scope, "link")
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should parse metric" do
|
23
|
+
test_parse_line("1.1.0.0/16 dev eth0 scope link metric 1000", :metric, "1000")
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should parse proto" do
|
27
|
+
test_parse_line("1.0.0.0/24 dev eth0 proto kernel scope link src 1.0.1.1", :proto, "kernel")
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should parse src" do
|
31
|
+
test_parse_line("1.0.0.0/24 dev eth0 proto kernel scope link src 1.0.1.1", :src, "1.0.1.1")
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should parse type" do
|
35
|
+
test_parse_line("unreachable default dev lo table unspec proto kernel metric -1 error -101", :type, "unreachable")
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should parse table" do
|
39
|
+
test_parse_line("unreachable default dev lo table unspec proto kernel metric -1 error -101", :table, "unspec")
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should parse error" do
|
43
|
+
test_parse_line("unreachable default dev lo table unspec proto kernel metric -1 error -101", :error, "-101")
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
context "parser" do
|
48
|
+
def test_parse(file, expect)
|
49
|
+
Net::IP::RouteParser.parse(File.read(file)).should eql(expect)
|
50
|
+
end
|
51
|
+
|
52
|
+
it "handles sample 1" do
|
53
|
+
test_parse("spec/net/ip/sample1", [
|
54
|
+
{:prefix=>"default", :dev=>"eth0", :proto=>"static", :via=>"192.168.0.1"},
|
55
|
+
{:prefix=>"169.254.0.0/16", :dev=>"eth0", :scope=>"link", :metric=>"1000"},
|
56
|
+
{:prefix=>"192.168.0.0/24", :dev=>"eth0", :scope=>"link", :metric=>"1", :proto=>"kernel", :src=>"192.168.0.15"}
|
57
|
+
])
|
58
|
+
end
|
59
|
+
|
60
|
+
it "handles sample 2" do
|
61
|
+
test_parse("spec/net/ip/sample2", [
|
62
|
+
{:prefix=>"default", :dev=>"eth0", :via=>"10.0.61.225", :weight=>"1"},
|
63
|
+
{:prefix=>"default", :dev=>"eth0", :via=>"10.0.45.153", :weight=>"1"},
|
64
|
+
{:prefix=>"10.0.0.0", :dev=>"eth0", :via=>"10.0.0.1"},
|
65
|
+
{:prefix=>"10.0.0.0/18", :dev=>"eth0", :scope=>"link", :proto=>"kernel", :src=>"10.0.14.184"},
|
66
|
+
{:prefix=>"169.254.169.254", :dev=>"eth0", :via=>"10.0.0.1"}
|
67
|
+
])
|
68
|
+
end
|
69
|
+
|
70
|
+
it "handles sample 3" do
|
71
|
+
test_parse("spec/net/ip/sample3", [
|
72
|
+
{:prefix=>"default", :dev=>"eth0", :via=>"10.0.35.196"},
|
73
|
+
{:prefix=>"10.0.0.0", :dev=>"eth0", :via=>"10.0.0.1"},
|
74
|
+
{:prefix=>"10.0.0.0/18", :dev=>"eth0", :scope=>"link", :proto=>"kernel", :src=>"10.0.58.155"},
|
75
|
+
{:prefix=>"169.254.169.254", :dev=>"eth0", :via=>"10.0.0.1"}
|
76
|
+
])
|
77
|
+
end
|
78
|
+
|
79
|
+
it "handles sample 4" do
|
80
|
+
test_parse("spec/net/ip/sample4", [
|
81
|
+
{:prefix=>"default", :dev=>"eth0", :via=>"192.168.0.1"},
|
82
|
+
{:prefix=>"169.254.0.0/16", :dev=>"eth0", :scope=>"link", :metric=>"1000"},
|
83
|
+
{:prefix=>"192.168.0.0/24", :dev=>"eth0", :scope=>"link", :metric=>"1", :proto=>"kernel", :src=>"192.168.0.15"},
|
84
|
+
{:type=>"broadcast", :prefix=>"127.0.0.0", :dev=>"lo", :scope=>"link", :proto=>"kernel", :src=>"127.0.0.1", :table=>"local"},
|
85
|
+
{:type=>"local", :prefix=>"127.0.0.0/8", :dev=>"lo", :scope=>"host", :proto=>"kernel", :src=>"127.0.0.1", :table=>"local"},
|
86
|
+
{:type=>"local", :prefix=>"127.0.0.1", :dev=>"lo", :scope=>"host", :proto=>"kernel", :src=>"127.0.0.1", :table=>"local"},
|
87
|
+
{:type=>"broadcast", :prefix=>"127.255.255.255", :dev=>"lo", :scope=>"link", :proto=>"kernel", :src=>"127.0.0.1", :table=>"local"},
|
88
|
+
{:type=>"broadcast", :prefix=>"192.168.0.0", :dev=>"eth0", :scope=>"link", :proto=>"kernel", :src=>"192.168.0.15", :table=>"local"},
|
89
|
+
{:type=>"local", :prefix=>"192.168.0.15", :dev=>"eth0", :scope=>"host", :proto=>"kernel", :src=>"192.168.0.15", :table=>"local"},
|
90
|
+
{:type=>"broadcast", :prefix=>"192.168.0.255", :dev=>"eth0", :scope=>"link", :proto=>"kernel", :src=>"192.168.0.15", :table=>"local"},
|
91
|
+
{:prefix=>"fe80::/64", :dev=>"eth0", :metric=>"256", :proto=>"kernel"},
|
92
|
+
{:type=>"unreachable", :prefix=>"default", :dev=>"lo", :metric=>"-1", :proto=>"kernel", :table=>"unspec", :error=>"-101"},
|
93
|
+
{:type=>"local", :prefix=>"::1", :dev=>"lo", :metric=>"0", :proto=>"none", :via=>"::", :table=>"local"},
|
94
|
+
{:type=>"local", :prefix=>"fe80::226:18ff:fe3a:3112", :dev=>"lo", :metric=>"0", :proto=>"none", :via=>"::", :table=>"local"},
|
95
|
+
{:prefix=>"ff00::/8", :dev=>"eth0", :metric=>"256", :table=>"local"},
|
96
|
+
{:type=>"unreachable", :prefix=>"default", :dev=>"lo", :metric=>"-1", :proto=>"kernel", :table=>"unspec", :error=>"-101"}
|
97
|
+
])
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
data/spec/net/ip/sample1
ADDED
data/spec/net/ip/sample2
ADDED
data/spec/net/ip/sample3
ADDED
data/spec/net/ip/sample4
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
default via 192.168.0.1 dev eth0
|
2
|
+
169.254.0.0/16 dev eth0 scope link metric 1000
|
3
|
+
192.168.0.0/24 dev eth0 proto kernel scope link src 192.168.0.15 metric 1
|
4
|
+
broadcast 127.0.0.0 dev lo table local proto kernel scope link src 127.0.0.1
|
5
|
+
local 127.0.0.0/8 dev lo table local proto kernel scope host src 127.0.0.1
|
6
|
+
local 127.0.0.1 dev lo table local proto kernel scope host src 127.0.0.1
|
7
|
+
broadcast 127.255.255.255 dev lo table local proto kernel scope link src 127.0.0.1
|
8
|
+
broadcast 192.168.0.0 dev eth0 table local proto kernel scope link src 192.168.0.15
|
9
|
+
local 192.168.0.15 dev eth0 table local proto kernel scope host src 192.168.0.15
|
10
|
+
broadcast 192.168.0.255 dev eth0 table local proto kernel scope link src 192.168.0.15
|
11
|
+
fe80::/64 dev eth0 proto kernel metric 256
|
12
|
+
unreachable default dev lo table unspec proto kernel metric -1 error -101
|
13
|
+
local ::1 via :: dev lo table local proto none metric 0
|
14
|
+
local fe80::226:18ff:fe3a:3112 via :: dev lo table local proto none metric 0
|
15
|
+
ff00::/8 dev eth0 table local metric 256
|
16
|
+
unreachable default dev lo table unspec proto kernel metric -1 error -101
|
metadata
CHANGED
@@ -1,109 +1,129 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: net-ip
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 23
|
5
5
|
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 4
|
10
|
+
version: 0.0.4
|
6
11
|
platform: ruby
|
7
|
-
authors:
|
12
|
+
authors:
|
8
13
|
- Joshua Bussdieker
|
9
14
|
autorequire:
|
10
15
|
bindir: bin
|
11
16
|
cert_chain: []
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
+
|
18
|
+
date: 2013-08-09 00:00:00 Z
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
version_requirements: &id001 !ruby/object:Gem::Requirement
|
17
22
|
none: false
|
18
|
-
requirements:
|
23
|
+
requirements:
|
19
24
|
- - ~>
|
20
|
-
- !ruby/object:Gem::Version
|
21
|
-
|
22
|
-
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
hash: 9
|
27
|
+
segments:
|
28
|
+
- 1
|
29
|
+
- 3
|
30
|
+
version: "1.3"
|
31
|
+
name: bundler
|
23
32
|
prerelease: false
|
24
|
-
|
33
|
+
type: :development
|
34
|
+
requirement: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
version_requirements: &id002 !ruby/object:Gem::Requirement
|
25
37
|
none: false
|
26
|
-
requirements:
|
27
|
-
- -
|
28
|
-
- !ruby/object:Gem::Version
|
29
|
-
|
30
|
-
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
hash: 3
|
42
|
+
segments:
|
43
|
+
- 0
|
44
|
+
version: "0"
|
31
45
|
name: rake
|
32
|
-
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
|
-
requirements:
|
35
|
-
- - ! '>='
|
36
|
-
- !ruby/object:Gem::Version
|
37
|
-
version: '0'
|
38
|
-
type: :development
|
39
46
|
prerelease: false
|
40
|
-
|
47
|
+
type: :development
|
48
|
+
requirement: *id002
|
49
|
+
- !ruby/object:Gem::Dependency
|
50
|
+
version_requirements: &id003 !ruby/object:Gem::Requirement
|
41
51
|
none: false
|
42
|
-
requirements:
|
43
|
-
- -
|
44
|
-
- !ruby/object:Gem::Version
|
45
|
-
|
46
|
-
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
hash: 3
|
56
|
+
segments:
|
57
|
+
- 0
|
58
|
+
version: "0"
|
47
59
|
name: rspec
|
48
|
-
requirement: !ruby/object:Gem::Requirement
|
49
|
-
none: false
|
50
|
-
requirements:
|
51
|
-
- - ! '>='
|
52
|
-
- !ruby/object:Gem::Version
|
53
|
-
version: '0'
|
54
|
-
type: :development
|
55
60
|
prerelease: false
|
56
|
-
|
57
|
-
|
58
|
-
requirements:
|
59
|
-
- - ! '>='
|
60
|
-
- !ruby/object:Gem::Version
|
61
|
-
version: '0'
|
61
|
+
type: :development
|
62
|
+
requirement: *id003
|
62
63
|
description:
|
63
|
-
email:
|
64
|
+
email:
|
64
65
|
- jbussdieker@gmail.com
|
65
66
|
executables: []
|
67
|
+
|
66
68
|
extensions: []
|
69
|
+
|
67
70
|
extra_rdoc_files: []
|
68
|
-
|
71
|
+
|
72
|
+
files:
|
69
73
|
- .gitignore
|
74
|
+
- .travis.yml
|
70
75
|
- Gemfile
|
71
76
|
- LICENSE.txt
|
72
77
|
- README.md
|
73
78
|
- Rakefile
|
74
79
|
- lib/net/ip.rb
|
75
80
|
- lib/net/ip/route.rb
|
81
|
+
- lib/net/ip/route_parser.rb
|
76
82
|
- lib/net/ip/version.rb
|
77
83
|
- net-ip.gemspec
|
84
|
+
- spec/net/ip/route_parser_spec.rb
|
85
|
+
- spec/net/ip/route_spec.rb
|
86
|
+
- spec/net/ip/sample1
|
87
|
+
- spec/net/ip/sample2
|
88
|
+
- spec/net/ip/sample3
|
89
|
+
- spec/net/ip/sample4
|
78
90
|
homepage: http://github.com/jbussdieker/ruby-net-ip
|
79
|
-
licenses:
|
91
|
+
licenses:
|
80
92
|
- MIT
|
81
93
|
post_install_message:
|
82
94
|
rdoc_options: []
|
83
|
-
|
95
|
+
|
96
|
+
require_paths:
|
84
97
|
- lib
|
85
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
98
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
86
99
|
none: false
|
87
|
-
requirements:
|
88
|
-
- -
|
89
|
-
- !ruby/object:Gem::Version
|
90
|
-
|
91
|
-
segments:
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
hash: 3
|
104
|
+
segments:
|
92
105
|
- 0
|
93
|
-
|
94
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
106
|
+
version: "0"
|
107
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
95
108
|
none: false
|
96
|
-
requirements:
|
97
|
-
- -
|
98
|
-
- !ruby/object:Gem::Version
|
99
|
-
|
100
|
-
segments:
|
109
|
+
requirements:
|
110
|
+
- - ">="
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
hash: 3
|
113
|
+
segments:
|
101
114
|
- 0
|
102
|
-
|
115
|
+
version: "0"
|
103
116
|
requirements: []
|
117
|
+
|
104
118
|
rubyforge_project:
|
105
119
|
rubygems_version: 1.8.25
|
106
120
|
signing_key:
|
107
121
|
specification_version: 3
|
108
122
|
summary: Tools for working with IP routes
|
109
|
-
test_files:
|
123
|
+
test_files:
|
124
|
+
- spec/net/ip/route_parser_spec.rb
|
125
|
+
- spec/net/ip/route_spec.rb
|
126
|
+
- spec/net/ip/sample1
|
127
|
+
- spec/net/ip/sample2
|
128
|
+
- spec/net/ip/sample3
|
129
|
+
- spec/net/ip/sample4
|