interface_parser 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +103 -0
- data/Rakefile +1 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/interface_parser.gemspec +23 -0
- data/lib/interface_parser.rb +17 -0
- data/lib/interface_parser/base.rb +41 -0
- data/lib/interface_parser/ifconfig.rb +51 -0
- data/lib/interface_parser/ip_addr.rb +29 -0
- data/lib/interface_parser/version.rb +3 -0
- metadata +87 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: db50776c5ce332197f391c933405fb351c1040b0
|
4
|
+
data.tar.gz: d4617be34b008d658dea9a5ec7df10609ce73927
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f29ee1d947f6c0d82827e3bd9e6d60edd2070b71dd9339b10e06aed75477ec2b874dcde8270c11df010da0c6aff5c908aff778a452c4293cccd49eb9533ca9e2
|
7
|
+
data.tar.gz: 0aaba0eede71fffc70e7babcf07e6c374e0dfbe1bb90f22a0ccdda481fbb3cd7a4e8cbff1326824eeedb324ac0d342f8154788c4966373c7c28e74d48e39d13a
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 nownabe
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
# InterfaceParser
|
2
|
+
To parse interfaces from linux commands: ifconfig and ip.
|
3
|
+
|
4
|
+
## Installation
|
5
|
+
|
6
|
+
Add this line to your application's Gemfile:
|
7
|
+
|
8
|
+
```ruby
|
9
|
+
gem 'interface_parser'
|
10
|
+
```
|
11
|
+
|
12
|
+
And then execute:
|
13
|
+
|
14
|
+
$ bundle
|
15
|
+
|
16
|
+
Or install it yourself as:
|
17
|
+
|
18
|
+
$ gem install interface_parser
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
```ruby
|
22
|
+
pp InterfaceParser.parse_ifconfig(`ifconfig`)
|
23
|
+
|
24
|
+
# =>
|
25
|
+
{"eno16777984"=>
|
26
|
+
{"ipv4"=>"192.168.1.4",
|
27
|
+
"netmask"=>"255.255.255.0",
|
28
|
+
"broadcast"=>"192.168.1.255",
|
29
|
+
"ipv6"=>"xxx",
|
30
|
+
"mac"=>"06:a8:cc:02:34:41"},
|
31
|
+
"eno33554976"=>
|
32
|
+
{"ipv4"=>nil,
|
33
|
+
"netmask"=>nil,
|
34
|
+
"broadcast"=>nil,
|
35
|
+
"ipv6"=>nil,
|
36
|
+
"mac"=>"06:0a:a4:02:34:81"},
|
37
|
+
"eno50332200"=>
|
38
|
+
{"ipv4"=>"10.3.0.13",
|
39
|
+
"netmask"=>"255.255.248.0",
|
40
|
+
"broadcast"=>"10.3.7.255",
|
41
|
+
"ipv6"=>"fe80::3aff:fecc:113",
|
42
|
+
"mac"=>"02:00:3a:cc:01:13"},
|
43
|
+
"lo"=>
|
44
|
+
{"ipv4"=>"127.0.0.1",
|
45
|
+
"netmask"=>"255.0.0.0",
|
46
|
+
"broadcast"=>nil,
|
47
|
+
"ipv6"=>"::1",
|
48
|
+
"mac"=>nil}}
|
49
|
+
|
50
|
+
pp InterfaceParser.parse_ip_addr(`ip addr`)
|
51
|
+
|
52
|
+
# =>
|
53
|
+
{"lo"=>
|
54
|
+
{"index"=>"1",
|
55
|
+
"loopback"=>"00:00:00:00:00:00",
|
56
|
+
"mac"=>nil,
|
57
|
+
"ipv4"=>nil,
|
58
|
+
"ipv4_prefix"=>nil,
|
59
|
+
"broadcast"=>nil,
|
60
|
+
"ipv6"=>"::1",
|
61
|
+
"ipv6_prefix"=>"/128"},
|
62
|
+
"eno16777984"=>
|
63
|
+
{"index"=>"2",
|
64
|
+
"loopback"=>nil,
|
65
|
+
"mac"=>"06:a8:cc:02:34:41",
|
66
|
+
"ipv4"=>"192.168.1.4",
|
67
|
+
"ipv4_prefix"=>"/24",
|
68
|
+
"broadcast"=>"192.168.1.255",
|
69
|
+
"ipv6"=>"xxx",
|
70
|
+
"ipv6_prefix"=>"/64"},
|
71
|
+
"eno33554976"=>
|
72
|
+
{"index"=>"3",
|
73
|
+
"loopback"=>nil,
|
74
|
+
"mac"=>"06:0a:a4:02:34:81",
|
75
|
+
"ipv4"=>nil,
|
76
|
+
"ipv4_prefix"=>nil,
|
77
|
+
"broadcast"=>nil,
|
78
|
+
"ipv6"=>"fe80::40a:a4ff:fe02:3481",
|
79
|
+
"ipv6_prefix"=>"/64"},
|
80
|
+
"eno50332200"=>
|
81
|
+
{"index"=>"4",
|
82
|
+
"loopback"=>nil,
|
83
|
+
"mac"=>"02:00:3a:cc:01:13",
|
84
|
+
"ipv4"=>"10.3.0.13",
|
85
|
+
"ipv4_prefix"=>"/21",
|
86
|
+
"broadcast"=>"10.3.7.255",
|
87
|
+
"ipv6"=>"fe80::3aff:fecc:113",
|
88
|
+
"ipv6_prefix"=>"/64"}}
|
89
|
+
```
|
90
|
+
|
91
|
+
## Development
|
92
|
+
|
93
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
|
94
|
+
|
95
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
96
|
+
|
97
|
+
## Contributing
|
98
|
+
|
99
|
+
1. Fork it ( https://github.com/[my-github-username]/interface_parser/fork )
|
100
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
101
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
102
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
103
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "interface_parser"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'interface_parser/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "interface_parser"
|
8
|
+
spec.version = InterfaceParser::VERSION
|
9
|
+
spec.authors = ["nownabe"]
|
10
|
+
spec.email = ["nownabe@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{To parse interfaces from linux commands: ifconfig and ip.}
|
13
|
+
spec.homepage = "https://github.com/nownabe/interface_parser"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = "exe"
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.9"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require "interface_parser/version"
|
2
|
+
|
3
|
+
module InterfaceParser
|
4
|
+
class << self
|
5
|
+
def parse_ifconfig(command_result)
|
6
|
+
Ifconfig.parse(command_result)
|
7
|
+
end
|
8
|
+
|
9
|
+
def parse_ip_addr(command_result)
|
10
|
+
IpAddr.parse(command_result)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
require "interface_parser/base"
|
16
|
+
require "interface_parser/ifconfig"
|
17
|
+
require "interface_parser/ip_addr"
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module InterfaceParser
|
2
|
+
class Base
|
3
|
+
attr_reader :command_result
|
4
|
+
|
5
|
+
def self.parse(command_result)
|
6
|
+
new(command_result).parse
|
7
|
+
end
|
8
|
+
|
9
|
+
def initialize(command_result)
|
10
|
+
@command_result = command_result
|
11
|
+
end
|
12
|
+
|
13
|
+
def parse
|
14
|
+
interfaces
|
15
|
+
end
|
16
|
+
|
17
|
+
def raw_interfaces
|
18
|
+
@raw_interfaces ||= split_interfaces
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def beginning_of_interface
|
24
|
+
self.class::BEGINNING_OF_INTERFACE
|
25
|
+
end
|
26
|
+
|
27
|
+
def split_interfaces
|
28
|
+
interfaces = []
|
29
|
+
interface = ""
|
30
|
+
command_result.each_line do |line|
|
31
|
+
if interface != "" && beginning_of_interface =~ line
|
32
|
+
interfaces << interface
|
33
|
+
interface = ""
|
34
|
+
end
|
35
|
+
interface << line unless /^$/ =~ line
|
36
|
+
end
|
37
|
+
interfaces << interface if interface != ""
|
38
|
+
interfaces
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module InterfaceParser
|
2
|
+
class Ifconfig < Base
|
3
|
+
BEGINNING_OF_INTERFACE = /^[^\s]+:\s/m
|
4
|
+
|
5
|
+
def interfaces
|
6
|
+
@interfaces ||= raw_interfaces.inject({}) do |interfaces, raw_interface|
|
7
|
+
interfaces[get_name(raw_interface)] = {
|
8
|
+
"ipv4" => get_ipv4(raw_interface),
|
9
|
+
"netmask" => get_netmask(raw_interface),
|
10
|
+
"broadcast" => get_broadcast(raw_interface),
|
11
|
+
"ipv6" => get_ipv6(raw_interface),
|
12
|
+
"mac" => get_mac(raw_interface)
|
13
|
+
}
|
14
|
+
|
15
|
+
interfaces
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def get_broadcast(raw_interface)
|
22
|
+
/broadcast (?<broadcast>[\d\.]+)/ =~ raw_interface
|
23
|
+
broadcast
|
24
|
+
end
|
25
|
+
|
26
|
+
def get_ipv4(raw_interface)
|
27
|
+
/inet (?<ipv4>[\d\.]+)/ =~ raw_interface
|
28
|
+
ipv4
|
29
|
+
end
|
30
|
+
|
31
|
+
def get_ipv6(raw_interface)
|
32
|
+
/inet6 (?<ipv6>[\w:%]+)/ =~ raw_interface
|
33
|
+
ipv6
|
34
|
+
end
|
35
|
+
|
36
|
+
def get_mac(raw_interface)
|
37
|
+
/ether (?<mac>[\w:]+)\s/ =~ raw_interface
|
38
|
+
mac
|
39
|
+
end
|
40
|
+
|
41
|
+
def get_netmask(raw_interface)
|
42
|
+
/netmask (?<netmask>[\d\.xa-f]+)/ =~ raw_interface
|
43
|
+
netmask
|
44
|
+
end
|
45
|
+
|
46
|
+
def get_name(raw_interface)
|
47
|
+
/^(?<name>[^\s]+):/ =~ raw_interface
|
48
|
+
name
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module InterfaceParser
|
2
|
+
class IpAddr < Base
|
3
|
+
BEGINNING_OF_INTERFACE = /^\d+:/m
|
4
|
+
|
5
|
+
def interfaces
|
6
|
+
@interfaces ||= raw_interfaces.inject({}) do |interfaces, raw_interface|
|
7
|
+
/^(?<index>\d+):/ =~ raw_interface
|
8
|
+
/loopback (?<loopback>[\w:]+)/ =~ raw_interface
|
9
|
+
/ether (?<mac>[\w:]+)/ =~ raw_interface
|
10
|
+
/inet (?<ipv4>[\d\.]+)(?<ipv4_prefix>\/\d\d?) brd (?<broadcast>[\d\.]+)/ =~ raw_interface
|
11
|
+
/inet6 (?<ipv6>[\w:]+)(?<ipv6_prefix>\/\d\d?\d?)/ =~ raw_interface
|
12
|
+
/^\d+: (?<name>[^:]+):/ =~ raw_interface
|
13
|
+
|
14
|
+
interfaces[name] = {
|
15
|
+
"index" => index,
|
16
|
+
"loopback" => loopback,
|
17
|
+
"mac" => mac,
|
18
|
+
"ipv4" => ipv4,
|
19
|
+
"ipv4_prefix" => ipv4_prefix,
|
20
|
+
"broadcast" => broadcast,
|
21
|
+
"ipv6" => ipv6,
|
22
|
+
"ipv6_prefix" => ipv6_prefix
|
23
|
+
}
|
24
|
+
|
25
|
+
interfaces
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
metadata
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: interface_parser
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- nownabe
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-06-02 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.9'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.9'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
description:
|
42
|
+
email:
|
43
|
+
- nownabe@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- ".rspec"
|
50
|
+
- ".travis.yml"
|
51
|
+
- Gemfile
|
52
|
+
- LICENSE.txt
|
53
|
+
- README.md
|
54
|
+
- Rakefile
|
55
|
+
- bin/console
|
56
|
+
- bin/setup
|
57
|
+
- interface_parser.gemspec
|
58
|
+
- lib/interface_parser.rb
|
59
|
+
- lib/interface_parser/base.rb
|
60
|
+
- lib/interface_parser/ifconfig.rb
|
61
|
+
- lib/interface_parser/ip_addr.rb
|
62
|
+
- lib/interface_parser/version.rb
|
63
|
+
homepage: https://github.com/nownabe/interface_parser
|
64
|
+
licenses:
|
65
|
+
- MIT
|
66
|
+
metadata: {}
|
67
|
+
post_install_message:
|
68
|
+
rdoc_options: []
|
69
|
+
require_paths:
|
70
|
+
- lib
|
71
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '0'
|
81
|
+
requirements: []
|
82
|
+
rubyforge_project:
|
83
|
+
rubygems_version: 2.4.5
|
84
|
+
signing_key:
|
85
|
+
specification_version: 4
|
86
|
+
summary: 'To parse interfaces from linux commands: ifconfig and ip.'
|
87
|
+
test_files: []
|