arpdb 0.1.7 → 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -30,17 +30,17 @@ include Arpdb
30
30
  devices = %w(10.40.5.1 10.43.5.1)
31
31
  community = 'public'
32
32
 
33
- a = Arp.new(devices, community)
33
+ adb = Arp.new(devices, community)
34
34
 
35
- a.scan
35
+ adb.scan
36
36
 
37
- puts a.mac_to_ip "7a70dec81b02"
38
- puts a.ip_to_mac "172.27.50.2"
37
+ puts adb.mac_to_ip "7a70dec81b02"
38
+ puts adb.ip_to_mac "172.27.50.2"
39
39
 
40
- puts a.locate_mac "7a70dec81b02"
41
- puts a.locate_ip "172.27.50.2"
40
+ puts adb.locate_mac "7a70dec81b02"
41
+ puts adb.locate_ip "172.27.50.2"
42
42
 
43
- a.refresh
43
+ adb.refresh
44
44
 
45
45
  ```
46
46
 
data/arpdb.gemspec CHANGED
@@ -30,6 +30,5 @@ Gem::Specification.new do |spec|
30
30
  spec.add_development_dependency "pry"
31
31
 
32
32
  spec.add_dependency "snmp", '~> 1.2'
33
- spec.add_dependency "awesome_print", '~> 1.6'
34
33
 
35
34
  end
data/bin/console CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  require "bundler/setup"
4
4
  require "arpdb"
5
+ include Arpdb
5
6
 
6
7
  # reload a gem
7
8
  def reload(require_regex)
data/lib/arpdb/arp.rb CHANGED
@@ -1,10 +1,7 @@
1
- require 'snmp'
2
- require 'ap'
3
-
4
- # Class to fetch ARP tables from Juniper firewalls, maintain them in
5
- # memory and perform search operations on the cached data.
6
1
  module Arpdb
7
2
 
3
+ # Class to fetch ARP tables from Juniper firewalls, maintain them in
4
+ # memory and perform search operations on the cached data.
8
5
  class Arp
9
6
 
10
7
  class ArpdbError < StandardError
@@ -12,81 +9,74 @@ module Arpdb
12
9
 
13
10
  attr_accessor :db
14
11
 
15
- # * +hostlist+ - Array of hostnames (strings) who's ARP tables to fetch
16
12
  def initialize(hostlist, community = 'public')
17
13
  @hostlist = hostlist
18
14
  @community = community
19
15
  @db = Array.new
20
16
  end
21
17
 
22
- # Just an alias for scan
23
- def refresh
18
+ def rescan
24
19
  scan
25
20
  end
26
21
 
27
22
  def scan
28
23
  @hostlist.each do |host|
29
24
  handle_exceptions do
30
- SNMP::Manager.open(host: host, community: @community, mib_modules: [], retries: 1) do |manager|
31
-
32
- location = String.new
33
- manager.get('1.3.6.1.2.1.1.6.0').each_varbind { |vb| location = vb.value }
25
+ st = SNMPTransport.new(host, @community)
26
+ location = st.get('1.3.6.1.2.1.1.6.0')
34
27
 
35
- manager.walk(%w(1.3.6.1.2.1.4.22.1.2 1.3.6.1.2.1.4.22.1.3)) do |row|
36
- mac = row[0].value.unpack('H*').first
37
- ip = row[1].value.to_s
38
- @db << {mac: mac, ip: ip, host: host, location: location}
39
- end
28
+ st.walk(%w(1.3.6.1.2.1.4.22.1.2 1.3.6.1.2.1.4.22.1.3)).each do |row|
29
+ @db << {mac: row.first, ip: row.last, host: host, location: location}
40
30
  end
31
+ st.close
41
32
  end
33
+
42
34
  end
43
35
  self
44
36
  end
45
37
 
46
- # * +mac+ - MAC address. String.
47
- # mac_to_ip("a7fea790ffa9")
48
38
  def mac_to_ip(mac)
39
+ return '' if mac.nil?
40
+
49
41
  db.each do |line|
50
42
  if line[:mac].eql?(mac.downcase.gsub(':', ''))
51
43
  return line[:ip]
52
44
  end
53
45
  end
54
- ''
46
+ String.new
55
47
  end
56
48
 
57
- # * +ip+ - IP address. String, decimal notation.
58
- # ip_to_mac("10.0.0.1")
59
49
  def ip_to_mac(ip)
50
+ return '' if ip.nil?
51
+
60
52
  db.each do |line|
61
53
  if line[:ip].eql?(ip)
62
54
  return line[:mac]
63
55
  end
64
56
  end
65
- ''
57
+ String.new
66
58
  end
67
59
 
68
- # Returns the syslocation that has given MAC in it's ARP table
69
- # * +mac+ - MAC address. String, hex, lowercase, no byte separators.
70
- # locate_mac("a7fea790ffa9")
71
60
  def locate_mac(mac)
61
+ return '' if mac.nil?
62
+
72
63
  db.each do |line|
73
64
  if line[:mac].eql?(mac.downcase.gsub(':', ''))
74
65
  return line[:location]
75
66
  end
76
67
  end
77
- ''
68
+ String.new
78
69
  end
79
70
 
80
- # Returns the syslocation that has given IP in it's ARP table
81
- # * +ip+ - IP address. String, decimal.
82
- # locate_mac("10.0.0.1")
83
71
  def locate_ip(ip)
72
+ return '' if ip.nil?
73
+
84
74
  db.each do |line|
85
75
  if line[:ip].eql?(ip)
86
76
  return line[:location]
87
77
  end
88
78
  end
89
- ''
79
+ String.new
90
80
  end
91
81
 
92
82
  private
@@ -95,7 +85,7 @@ module Arpdb
95
85
  begin
96
86
  yield
97
87
  rescue => e
98
- raise ArpdbError, "Exception in Arpdb::Arp: #{e.to_s}"
88
+ raise ArpdbError, "Exception in Arpdb::Arp: #{e}"
99
89
  end
100
90
  end
101
91
 
@@ -0,0 +1,43 @@
1
+ require 'snmp'
2
+
3
+ class SNMPTransport
4
+
5
+ attr_accessor :manager
6
+
7
+ def initialize(host, community = 'public')
8
+ @manager = SNMP::Manager.new(host: host, community: community, mib_modules: [], retries: 1)
9
+ end
10
+
11
+ def walk(oids)
12
+ result = Array.new
13
+ manager.walk(oids) do |snmp_row|
14
+ row = Array.new
15
+ snmp_row.each do |vb|
16
+ row << decode_value(vb)
17
+ end
18
+ result << row
19
+ end
20
+ result
21
+ end
22
+
23
+ def get(oid)
24
+ manager.get(oid).each_varbind { |vb| return decode_value(vb) }
25
+ end
26
+
27
+ def close
28
+ manager.close
29
+ end
30
+
31
+ private
32
+
33
+ def decode_value(vb)
34
+ # Absolutely ugly hack to decide if value needs to be unpacked.
35
+ # Add OIDs known to return packed/binary data here.
36
+ # Please recommend correct solution for this.
37
+ if vb.name.to_s =~ /1\.3\.6\.1\.2\.1\.4\.22\.1\.2/
38
+ vb.value.unpack('H*').first
39
+ else
40
+ vb.value.to_s
41
+ end
42
+ end
43
+ end
data/lib/arpdb/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Arpdb
2
- VERSION = "0.1.7"
2
+ VERSION = "0.1.8"
3
3
  end
data/lib/arpdb.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  require "arpdb/version"
2
2
  require "arpdb/arp"
3
-
3
+ require 'arpdb/snmp_transport'
4
4
 
5
5
  module Arpdb
6
6
  end
metadata CHANGED
@@ -1,141 +1,144 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: arpdb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.1.8
5
+ prerelease:
5
6
  platform: ruby
6
7
  authors:
7
8
  - J.K.Valk
8
9
  autorequire:
9
10
  bindir: exe
10
11
  cert_chain: []
11
- date: 2015-06-15 00:00:00.000000000 Z
12
+ date: 2015-06-17 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: bundler
15
16
  requirement: !ruby/object:Gem::Requirement
17
+ none: false
16
18
  requirements:
17
- - - "~>"
19
+ - - ~>
18
20
  - !ruby/object:Gem::Version
19
21
  version: '1.10'
20
22
  type: :development
21
23
  prerelease: false
22
24
  version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
23
26
  requirements:
24
- - - "~>"
27
+ - - ~>
25
28
  - !ruby/object:Gem::Version
26
29
  version: '1.10'
27
30
  - !ruby/object:Gem::Dependency
28
31
  name: rake
29
32
  requirement: !ruby/object:Gem::Requirement
33
+ none: false
30
34
  requirements:
31
- - - "~>"
35
+ - - ~>
32
36
  - !ruby/object:Gem::Version
33
37
  version: '10.0'
34
38
  type: :development
35
39
  prerelease: false
36
40
  version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
37
42
  requirements:
38
- - - "~>"
43
+ - - ~>
39
44
  - !ruby/object:Gem::Version
40
45
  version: '10.0'
41
46
  - !ruby/object:Gem::Dependency
42
47
  name: rspec
43
48
  requirement: !ruby/object:Gem::Requirement
49
+ none: false
44
50
  requirements:
45
- - - ">="
51
+ - - ! '>='
46
52
  - !ruby/object:Gem::Version
47
53
  version: '0'
48
54
  type: :development
49
55
  prerelease: false
50
56
  version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
51
58
  requirements:
52
- - - ">="
59
+ - - ! '>='
53
60
  - !ruby/object:Gem::Version
54
61
  version: '0'
55
62
  - !ruby/object:Gem::Dependency
56
63
  name: guard
57
64
  requirement: !ruby/object:Gem::Requirement
65
+ none: false
58
66
  requirements:
59
- - - ">="
67
+ - - ! '>='
60
68
  - !ruby/object:Gem::Version
61
69
  version: '0'
62
70
  type: :development
63
71
  prerelease: false
64
72
  version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
65
74
  requirements:
66
- - - ">="
75
+ - - ! '>='
67
76
  - !ruby/object:Gem::Version
68
77
  version: '0'
69
78
  - !ruby/object:Gem::Dependency
70
79
  name: guard-rspec
71
80
  requirement: !ruby/object:Gem::Requirement
81
+ none: false
72
82
  requirements:
73
- - - ">="
83
+ - - ! '>='
74
84
  - !ruby/object:Gem::Version
75
85
  version: '0'
76
86
  type: :development
77
87
  prerelease: false
78
88
  version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
79
90
  requirements:
80
- - - ">="
91
+ - - ! '>='
81
92
  - !ruby/object:Gem::Version
82
93
  version: '0'
83
94
  - !ruby/object:Gem::Dependency
84
95
  name: coveralls
85
96
  requirement: !ruby/object:Gem::Requirement
97
+ none: false
86
98
  requirements:
87
- - - ">="
99
+ - - ! '>='
88
100
  - !ruby/object:Gem::Version
89
101
  version: '0'
90
102
  type: :development
91
103
  prerelease: false
92
104
  version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
93
106
  requirements:
94
- - - ">="
107
+ - - ! '>='
95
108
  - !ruby/object:Gem::Version
96
109
  version: '0'
97
110
  - !ruby/object:Gem::Dependency
98
111
  name: pry
99
112
  requirement: !ruby/object:Gem::Requirement
113
+ none: false
100
114
  requirements:
101
- - - ">="
115
+ - - ! '>='
102
116
  - !ruby/object:Gem::Version
103
117
  version: '0'
104
118
  type: :development
105
119
  prerelease: false
106
120
  version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
107
122
  requirements:
108
- - - ">="
123
+ - - ! '>='
109
124
  - !ruby/object:Gem::Version
110
125
  version: '0'
111
126
  - !ruby/object:Gem::Dependency
112
127
  name: snmp
113
128
  requirement: !ruby/object:Gem::Requirement
129
+ none: false
114
130
  requirements:
115
- - - "~>"
131
+ - - ~>
116
132
  - !ruby/object:Gem::Version
117
133
  version: '1.2'
118
134
  type: :runtime
119
135
  prerelease: false
120
136
  version_requirements: !ruby/object:Gem::Requirement
137
+ none: false
121
138
  requirements:
122
- - - "~>"
139
+ - - ~>
123
140
  - !ruby/object:Gem::Version
124
141
  version: '1.2'
125
- - !ruby/object:Gem::Dependency
126
- name: awesome_print
127
- requirement: !ruby/object:Gem::Requirement
128
- requirements:
129
- - - "~>"
130
- - !ruby/object:Gem::Version
131
- version: '1.6'
132
- type: :runtime
133
- prerelease: false
134
- version_requirements: !ruby/object:Gem::Requirement
135
- requirements:
136
- - - "~>"
137
- - !ruby/object:Gem::Version
138
- version: '1.6'
139
142
  description: This is VERY raw, version 1.0.0 is expected to be first usable implementation.
140
143
  email:
141
144
  - jkvalk@mail.ru
@@ -143,10 +146,10 @@ executables: []
143
146
  extensions: []
144
147
  extra_rdoc_files: []
145
148
  files:
146
- - ".coveralls.yml"
147
- - ".gitignore"
148
- - ".rspec"
149
- - ".travis.yml"
149
+ - .coveralls.yml
150
+ - .gitignore
151
+ - .rspec
152
+ - .travis.yml
150
153
  - CODE_OF_CONDUCT.md
151
154
  - Gemfile
152
155
  - Guardfile
@@ -157,30 +160,32 @@ files:
157
160
  - bin/setup
158
161
  - lib/arpdb.rb
159
162
  - lib/arpdb/arp.rb
163
+ - lib/arpdb/snmp_transport.rb
160
164
  - lib/arpdb/version.rb
161
165
  homepage: https://github.com/jkvalk/arpdb
162
166
  licenses:
163
167
  - MIT
164
- metadata: {}
165
168
  post_install_message:
166
169
  rdoc_options: []
167
170
  require_paths:
168
171
  - lib
169
172
  required_ruby_version: !ruby/object:Gem::Requirement
173
+ none: false
170
174
  requirements:
171
- - - ">="
175
+ - - ! '>='
172
176
  - !ruby/object:Gem::Version
173
177
  version: '0'
174
178
  required_rubygems_version: !ruby/object:Gem::Requirement
179
+ none: false
175
180
  requirements:
176
- - - ">="
181
+ - - ! '>='
177
182
  - !ruby/object:Gem::Version
178
183
  version: '0'
179
184
  requirements: []
180
185
  rubyforge_project:
181
- rubygems_version: 2.2.2
186
+ rubygems_version: 1.8.23
182
187
  signing_key:
183
- specification_version: 4
188
+ specification_version: 3
184
189
  summary: Arpdb is a Ruby gem for querying SNMP-enabled network devices for their ARP
185
190
  database.
186
191
  test_files: []
checksums.yaml DELETED
@@ -1,7 +0,0 @@
1
- ---
2
- SHA1:
3
- metadata.gz: b7c753c2901c8bd2bea9d25b0a0d203ebe7104ef
4
- data.tar.gz: a554e546f06dcce5cd2aa97e848ef8f60978bc43
5
- SHA512:
6
- metadata.gz: a5cee996af101ddc77df8d95de71187a5ecb3610cc64286bd1418f0091cf538b0e6ef04a356b12ab02dd0e6ff0c0e0a48565b9efd98de34acc01f271c48d39c4
7
- data.tar.gz: 9aef88bb92e18112d9044c59916de096179f316ad8807cb284527c75f327d26cbf98a3f6179318e25e3ef2a405cf21fae21c568ef7a2b215cd08749b71c01360