netdisco 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ded71de342a66250ded62a3f156f5d682bf49e9e32d9b4e1dad2c29b43a2e8fc
4
- data.tar.gz: fac4f4eba715ced9b239193d443012d0c73358cc9144871952a1c95595695f71
3
+ metadata.gz: 717c023a9db51e3af00801112bbeaf47ab48159b98db46932bfd03243fcd5965
4
+ data.tar.gz: 5c871ee533134fd8e9e6e840c2cb6b598b031b013979247ef529d1565d2cc7ad
5
5
  SHA512:
6
- metadata.gz: 326132153b188f6383d596340638eb0da1e3126af112d5e395b4beda8be3ccb54cd2cfe37b07615a466973f9ba7c26239305e569e3780f6340f3a20436a60979
7
- data.tar.gz: 7b289018e3e85556932a812632a014733b15b579134911e7aa3dfcd9fad794d7d4c79ef40d5507bdb14ff732adc6ad37489362bb96f213b1e95c6b39e616e79b
6
+ metadata.gz: 5f2bdf61addfe71f2bdcdeb6b0728ae36fc9453f9edcce7e74c0c158bcf8bdcb4223ca8df25967c9ebfbfaaf65b2aebe7b7ba0f4a55e27bb1e946d95e49670e7
7
+ data.tar.gz: bfdabe4b30c8e3e5867b42ab9f6af9efa1748df080ed09cae4c37deef316086e72808a3bffc420152031fa0238dcc23fac133f864b0061e8e6b3cd5b706d63ee
data/lib/netdisco/cli.rb CHANGED
@@ -11,6 +11,7 @@ class Netdisco
11
11
 
12
12
  # 网络邻居发现入口函数
13
13
  def run
14
+ Log.debug "Running Netdisco.new.discover ..."
14
15
  output = Netdisco.new.discover @host
15
16
 
16
17
  # 邻居关系发现后修正处理逻辑钩子函数
@@ -9,14 +9,14 @@ class Netdisco
9
9
 
10
10
  # 设置默认参数
11
11
  CONFIG.default.use = %w(LLDP CDP)
12
- CONFIG.default.poll = %w[192.168.8.0/24]
12
+ CONFIG.default.poll = %w(192.168.8.0/24 10.250.8.0/24)
13
13
  CONFIG.default.snmp.community = "cisco"
14
14
  CONFIG.default.snmp.timeout = 15
15
15
  CONFIG.default.snmp.retries = 1
16
16
  CONFIG.default.snmp.bulkrows = 30 # 1500B packet should fit about 50 :cdpCacheAddress rows
17
17
  CONFIG.default.dot.bothlinks = false # keep both a-b and b-a links
18
18
  CONFIG.default.dot.linklabels = true # label link with interface names
19
- CONFIG.default.dot.color = [# regexp of host => color
19
+ CONFIG.default.dot.color = [ # regexp of host => color
20
20
  %w[cpe gold],
21
21
  %w[-sw blue],
22
22
  %w[-pe red],
@@ -25,18 +25,20 @@ class Netdisco
25
25
  CONFIG.default.dns.afi = nil # could be 'ipv4' or 'ipv6'
26
26
  CONFIG.default.log = "STDERR"
27
27
  CONFIG.default.debug = true
28
- CONFIG.default.name_map = [# regexp match+sub of hostname (needed for LLDP)
28
+ CONFIG.default.name_map = [ # regexp match+sub of hostname (needed for LLDP)
29
29
  ['-re\d+', ""],
30
30
  ["^KILLME(.*(?<!mojo.local)$)", '\1.mojo.local'], # adds missing domain name
31
31
  ]
32
32
  # 加载配置并设置对应常量
33
33
  CONFIG.load
34
34
  CFG = CONFIG.cfg
35
+
35
36
  # 设置项目日志参数
36
- log = CFG.log
37
- log = STDERR if log == "STDERR"
38
- log = STDOUT if log == "STDOUT"
39
- Log = Logger.new log
37
+ log = CFG.log
38
+ log = STDERR if log == "STDERR"
39
+ log = STDOUT if log == "STDOUT"
40
+ Log = Logger.new log
41
+
40
42
  Log.level = Logger::INFO
41
43
  Log.level = Logger::DEBUG if CFG.debug
42
44
  end
data/lib/netdisco/dns.rb CHANGED
@@ -26,8 +26,8 @@ class Netdisco
26
26
  else
27
27
  @cache_ip[name] = Resolv.getaddress name
28
28
  end
29
- rescue => error
30
- Log.debug "DNS resolution for '#{name}' raised error '#{error.class}' with message '#{error.message}'"
29
+ rescue StandardError => e
30
+ Log.debug "DNS resolution for '#{name}' raised error '#{e.class}' with message '#{e.message}'"
31
31
  nil
32
32
  end
33
33
  end
@@ -42,8 +42,8 @@ class Netdisco
42
42
  else
43
43
  begin
44
44
  @cache_name[ip] = Resolv.getname ip
45
- rescue => error
46
- Log.debug "DNS resolution for '#{ip}' raised error '#{error.class}' with message '#{error.message}'"
45
+ rescue StoreError => e
46
+ Log.debug "DNS resolution for '#{ip}' raised error '#{e.class}' with message '#{e.message}'"
47
47
  @cache_name[ip] = ip
48
48
  end
49
49
  end
@@ -37,7 +37,7 @@ class Netdisco
37
37
  peer.oid = get_oid_hash peer_id
38
38
  ip = @mib.by_partial(OID[:lldpRemManAddrIfSubtype], peer_id)
39
39
  peer.raw_ip = ip.oid[-4..-1].join(".") if ip # FIXME: IPv4 specific
40
- peer.raw_ip ||= "192.0.2.255" # sometimes we can't fnd any IP (EX2200 talking Arista found)
40
+ peer.raw_ip ||= "192.0.2.255" # sometimes we can't fnd any IP (EX2200 talking Arista found)
41
41
  peer.raw_name = @mib[OID[:lldpRemSysName], peer_id].value
42
42
  peer.ip = get_ip(peer.raw_ip, peer.raw_name)
43
43
  peer.dst = @mib[OID[:lldpRemPortId], peer_id].value
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "../snmp"
4
+ # require "logger"
4
5
 
5
6
  class Netdisco
6
7
  class XDP
@@ -75,20 +75,22 @@ class Netdisco
75
75
  def to_hash
76
76
  require "pp"
77
77
  out = ""
78
- PP.pp to_h, out
78
+ PP.pp(to_h, out)
79
79
  out
80
80
  end
81
81
 
82
- # @return [String] yaml of hosts and peers found
82
+ # @return [String, Instance] yaml of hosts and peers found
83
83
  def to_yaml
84
84
  require "yaml"
85
- YAML.dump to_h
85
+ File.open("./neighbors.yml", "w") { |file| file.write to_h.to_yaml }
86
+ YAML.dump(to_h)
86
87
  end
87
88
 
88
89
  # @return [String] json of hosts and peers found
89
90
  def to_json
90
91
  require "json"
91
- JSON.pretty_generate to_h
92
+ JSON.generate(to_h)
93
+ # JSON.pretty_generate to_h
92
94
  end
93
95
  end
94
96
  end
data/lib/netdisco/snmp.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "snmp"
2
4
 
3
5
  class Netdisco
@@ -74,8 +76,8 @@ class Netdisco
74
76
  private
75
77
  def snmp(method, *args)
76
78
  @snmp.send method, *args
77
- rescue ::SNMP::RequestTimeout, Errno::EACCES => error
78
- msg = "host '#{@host}' raised '#{error.class}' with message '#{error.message}' for method '#{method}' with args '#{args}'"
79
+ rescue ::SNMP::RequestTimeout, Errno::EACCES => e
80
+ msg = "host '#{@host}' raised '#{e.class}' with message '#{e.message}' for method '#{method}' with args '#{args}'"
79
81
  Log.warn msg
80
82
  raise NoResponse, msg
81
83
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Netdisco
4
- VERSION = "0.0.4"
4
+ VERSION = "0.0.5"
5
5
  end
data/lib/netdisco.rb CHANGED
@@ -59,7 +59,6 @@ class Netdisco
59
59
  # 第一步获取设备的邻居关系,同时将邻居关系缓存下来
60
60
  @hosts[host] = peers = neighbors(host)
61
61
  # 第二步遍历邻居的邻居关系
62
- require "pp"; PP.pp @hosts[host]
63
62
  peers.each do |peer|
64
63
  next if @hosts.has_key? peer.ip
65
64
  next unless @poll.include? peer.ip
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: netdisco
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - WENWU.YAN
@@ -42,16 +42,16 @@ dependencies:
42
42
  name: strada
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - "~>"
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
- version: 0.0.2
47
+ version: 0.0.3
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - "~>"
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
- version: 0.0.2
54
+ version: 0.0.3
55
55
  description: Netdisco is working for network snmp discovery
56
56
  email:
57
57
  - careline@foxmail.com