network-infra-utility 0.6.0 → 1.0.0
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.
- checksums.yaml +4 -4
- data/.gitignore +6 -0
- data/CHANGELOG.md +31 -0
- data/GUIDE.md +201 -212
- data/README.md +1 -6
- data/Rakefile +1 -1
- data/bin/gen-get +144 -0
- data/bin/geo-api +40 -1
- data/bin/geo-get +18 -36
- data/bin/ngeo-get +253 -0
- data/bin/packbit +227 -0
- data/bin/probe +134 -0
- data/document/reference/DNS/346/212/200/346/234/257/346/226/207/346/241/243.md +912 -0
- data/network-infra-utility.gemspec +6 -4
- data/network.rb +19 -1
- data/service/geodb/GeoAPI.md +64 -0
- data/service/geodb/api.rb +77 -19
- data/service/geoquery/GeoQuery.md +309 -0
- data/service/geoquery/cache.rb +78 -0
- data/service/geoquery/geo_cache.rb +233 -0
- data/service/geoquery/geoquery.rb +327 -0
- data/service/geoquery/local.rb +151 -0
- data/service/geoquery/merge.rb +52 -0
- data/service/geoquery/normalize.rb +102 -0
- data/service/geoquery/online.rb +136 -0
- data/service/simlab/SimLab.md +140 -0
- data/service/simlab/bin/simctl +152 -0
- data/service/simlab/lib/simlab.rb +124 -0
- data/service/simlab/lib/simlab_engine.rb +146 -0
- data/service/simlab/lib/simlab_observer.rb +176 -0
- data/service/simlab/lib/simlab_router.rb +330 -0
- data/service/simlab/lib/simlab_scenario.rb +200 -0
- data/service/simlab/lib/simlab_switch.rb +182 -0
- data/service/simlab/lib/simlab_topology.rb +196 -0
- data/service/simlab/lib/simlab_traffic.rb +167 -0
- data/support/basic/packet.rb +167 -0
- data/support/routing/bgp.rb +279 -0
- data/support/routing/lpm_trie.rb +145 -0
- data/support/routing/ospf.rb +243 -0
- data/support/routing/prefix.rb +155 -0
- data/support/routing/rib.rb +167 -0
- data/support/routing/rip.rb +223 -0
- data/support/routing/static_route.rb +102 -0
- data/support/switching/frame_forward.rb +87 -0
- data/support/switching/mac_table.rb +127 -0
- data/support/switching/stp.rb +230 -0
- data/support/switching/vlan.rb +155 -0
- data/tool/packbit/README.md +136 -0
- data/tool/packbit/config/five-tuple.yml +25 -0
- data/tool/packbit/config/flow.yml +17 -0
- data/tool/packbit/config/packbit.yml +41 -0
- data/tool/packbit/erl/packbit.erl +588 -0
- data/tool/probe/README.md +88 -0
- data/tool/probe/lib/probe.rb +54 -0
- data/tool/probe/lib/probe_base.rb +98 -0
- data/tool/probe/lib/probe_dns.rb +64 -0
- data/tool/probe/lib/probe_icmp.rb +71 -0
- data/tool/probe/lib/probe_ntp.rb +51 -0
- data/tool/probe/lib/probe_radius.rb +58 -0
- data/tool/probe/lib/probe_registry.rb +44 -0
- data/tool/probe/lib/probe_result.rb +75 -0
- data/tool/probe/lib/probe_snmp.rb +114 -0
- data/tool/probe/lib/probe_tcp.rb +82 -0
- data/tool/probe/lib/probe_version.rb +7 -0
- data/tool/webssh/README.md +42 -0
- data/tool/webssh/app.py +788 -0
- data/tool/webssh/cli.bat +17 -0
- data/tool/webssh/config.yml.template +84 -0
- data/tool/webssh/extension//350/256/276/345/244/207/350/257/212/346/226/255.md +103 -0
- data/tool/webssh/requirements.txt +7 -0
- data/tool/webssh/ssh_cli.py +449 -0
- data/tool/webssh/ssh_worker.py +782 -0
- data/tool/webssh/static/vendor/xterm-addon-fit.min.js +8 -0
- data/tool/webssh/static/vendor/xterm.css +209 -0
- data/tool/webssh/static/vendor/xterm.min.js +8 -0
- data/tool/webssh/templates/index.html +2674 -0
- data/tool/webssh/web.bat +41 -0
- data/version.rb +1 -1
- metadata +87 -2
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "probe_version"
|
|
4
|
+
require_relative "probe_result"
|
|
5
|
+
require_relative "probe_base"
|
|
6
|
+
require_relative "probe_registry"
|
|
7
|
+
|
|
8
|
+
# NetworkInfraUtility::Probe — 服务连通性探测工具箱
|
|
9
|
+
#
|
|
10
|
+
# 对网络/服务类设备探测各类服务的连通性,覆盖:
|
|
11
|
+
# icmp / telnet / ssh / netconf / snmp / twamp / dns / ntp / radius
|
|
12
|
+
#
|
|
13
|
+
# 用法:
|
|
14
|
+
# require "probe"
|
|
15
|
+
#
|
|
16
|
+
# # 单协议
|
|
17
|
+
# r = NetworkInfraUtility::Probe.run(:ssh, "10.0.0.1")
|
|
18
|
+
# r.ok? # → true / false
|
|
19
|
+
#
|
|
20
|
+
# # 批量
|
|
21
|
+
# results = NetworkInfraUtility::Probe.check("10.0.0.1", protocols: %i[ssh snmp dns], timeout: 3)
|
|
22
|
+
#
|
|
23
|
+
# # 全部协议
|
|
24
|
+
# NetworkInfraUtility::Probe.check("10.0.0.1")
|
|
25
|
+
#
|
|
26
|
+
# # 可用协议列表
|
|
27
|
+
# NetworkInfraUtility::Probe.protocols
|
|
28
|
+
module NetworkInfraUtility
|
|
29
|
+
module Probe
|
|
30
|
+
module_function
|
|
31
|
+
|
|
32
|
+
# 探测单个协议,返回 Result。
|
|
33
|
+
def run(protocol, host, **opts)
|
|
34
|
+
klass = Registry[protocol]
|
|
35
|
+
unless klass
|
|
36
|
+
raise ArgumentError,
|
|
37
|
+
"未知协议: #{protocol.inspect},可用: #{Registry.protocols.join(', ')}"
|
|
38
|
+
end
|
|
39
|
+
klass.new(host, **opts).call
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# 批量探测,返回 Result 数组。
|
|
43
|
+
# protocols 省略时探测全部已注册协议。
|
|
44
|
+
def check(host, protocols: nil, **opts)
|
|
45
|
+
list = protocols || Registry.protocols
|
|
46
|
+
list.map { |p| run(p, host, **opts) }
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# 全部可用协议名。
|
|
50
|
+
def protocols
|
|
51
|
+
Registry.protocols
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "socket"
|
|
4
|
+
require "rbconfig"
|
|
5
|
+
require_relative "probe_result"
|
|
6
|
+
|
|
7
|
+
module NetworkInfraUtility
|
|
8
|
+
module Probe
|
|
9
|
+
# 探测基类。所有协议探针继承本类,只需:
|
|
10
|
+
# 1. 实现 call(返回 Result)
|
|
11
|
+
# 2. 可选覆盖 default_port(类方法)
|
|
12
|
+
#
|
|
13
|
+
# protocol 名自动从类名派生:SshProbe → :ssh,DnsProbe → :dns。
|
|
14
|
+
class Base
|
|
15
|
+
attr_reader :host, :port, :timeout, :options
|
|
16
|
+
|
|
17
|
+
def initialize(host, port: nil, timeout: 3, **options)
|
|
18
|
+
@host = host
|
|
19
|
+
@port = port || self.class.default_port
|
|
20
|
+
@timeout = timeout.to_f
|
|
21
|
+
@options = options
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# 协议名(符号),由类名派生。
|
|
25
|
+
def self.protocol
|
|
26
|
+
name.split("::").last.sub(/Probe\z/, "").downcase.to_sym
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# 默认端口,子类覆盖。
|
|
30
|
+
def self.default_port
|
|
31
|
+
nil
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# 便捷入口:ProbeClass.run(host, ...) → Result
|
|
35
|
+
def self.run(host, **opts)
|
|
36
|
+
new(host, **opts).call
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# 子类必须实现。
|
|
40
|
+
def call
|
|
41
|
+
raise NotImplementedError, "#{self.class} 未实现 call"
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
private
|
|
45
|
+
|
|
46
|
+
# 构造统一 Result。
|
|
47
|
+
def result(status, latency: nil, message: nil, detail: nil)
|
|
48
|
+
Result.new(
|
|
49
|
+
protocol: self.class.protocol,
|
|
50
|
+
host: @host,
|
|
51
|
+
port: @port,
|
|
52
|
+
status: status,
|
|
53
|
+
latency: latency,
|
|
54
|
+
message: message,
|
|
55
|
+
detail: detail
|
|
56
|
+
)
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# 计时并返回 [yield 返回值, 耗时秒数]。
|
|
60
|
+
def timing
|
|
61
|
+
t0 = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
62
|
+
value = yield
|
|
63
|
+
[value, (Process.clock_gettime(Process::CLOCK_MONOTONIC) - t0).round(4)]
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# UDP 请求/响应交换:发送 payload。
|
|
67
|
+
# 返回:
|
|
68
|
+
# String → 收到响应(原始字节)
|
|
69
|
+
# nil → 超时无响应
|
|
70
|
+
# :refused → 端口拒绝/无监听(ICMP 不可达,ECONNREFUSED/ECONNRESET)
|
|
71
|
+
def udp_exchange(payload)
|
|
72
|
+
sock = UDPSocket.new
|
|
73
|
+
begin
|
|
74
|
+
sock.connect(@host, @port)
|
|
75
|
+
sock.send(payload, 0)
|
|
76
|
+
return nil unless IO.select([sock], nil, nil, @timeout)
|
|
77
|
+
sock.recvfrom_nonblock(4096).first
|
|
78
|
+
rescue Errno::ECONNREFUSED, Errno::ECONNRESET
|
|
79
|
+
:refused
|
|
80
|
+
ensure
|
|
81
|
+
sock.close if sock
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
# 从已连接 socket 读取至多 max 字节,带超时。
|
|
86
|
+
def read_with_timeout(sock, max = 1024, timeout: @timeout)
|
|
87
|
+
return nil unless IO.select([sock], nil, nil, timeout)
|
|
88
|
+
sock.recv_nonblock(max)
|
|
89
|
+
rescue IO::WaitReadable, EOFError, Errno::ECONNRESET, Errno::ECONNABORTED, Errno::EINVAL
|
|
90
|
+
nil
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def windows?
|
|
94
|
+
RbConfig::CONFIG["host_os"] =~ /mswin|mingw|cygwin|windows/
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
end
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "resolv"
|
|
4
|
+
require_relative "probe_base"
|
|
5
|
+
|
|
6
|
+
module NetworkInfraUtility
|
|
7
|
+
module Probe
|
|
8
|
+
# DNS 服务探测:向目标 DNS 服务器发起一次记录查询。
|
|
9
|
+
# 默认查询 example.com 的 A 记录。
|
|
10
|
+
class DnsProbe < Base
|
|
11
|
+
def self.default_port
|
|
12
|
+
53
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def call
|
|
16
|
+
query = @options.fetch(:name, "example.com")
|
|
17
|
+
type = @options.fetch(:type, "A")
|
|
18
|
+
answers, latency = timing { resolve(query, type) }
|
|
19
|
+
if answers && !answers.empty?
|
|
20
|
+
result(:ok, latency: latency, message: "DNS 响应正常", detail: "#{query} #{type.upcase} -> #{format_answer(answers.first)}")
|
|
21
|
+
else
|
|
22
|
+
result(:fail, latency: latency, message: "无有效响应")
|
|
23
|
+
end
|
|
24
|
+
rescue Resolv::ResolvError, Resolv::ResolvTimeout => e
|
|
25
|
+
result(:timeout, message: e.message)
|
|
26
|
+
rescue StandardError => e
|
|
27
|
+
result(:unknown, message: e.message)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
private
|
|
31
|
+
|
|
32
|
+
def resolve(query, type)
|
|
33
|
+
resolver = Resolv::DNS.new(nameserver: [@host], search: [], ndots: 1)
|
|
34
|
+
resolver.timeouts = [@timeout, @timeout]
|
|
35
|
+
resolver.getresources(query, resource_class(type))
|
|
36
|
+
ensure
|
|
37
|
+
resolver.close if resolver
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def resource_class(type)
|
|
41
|
+
case type.to_s.upcase
|
|
42
|
+
when "PTR" then Resolv::DNS::Resource::IN::PTR
|
|
43
|
+
when "AAAA" then Resolv::DNS::Resource::IN::AAAA
|
|
44
|
+
when "NS" then Resolv::DNS::Resource::IN::NS
|
|
45
|
+
when "MX" then Resolv::DNS::Resource::IN::MX
|
|
46
|
+
when "CNAME" then Resolv::DNS::Resource::IN::CNAME
|
|
47
|
+
else Resolv::DNS::Resource::IN::A
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# 记录 → 可读字符串。
|
|
52
|
+
def format_answer(rec)
|
|
53
|
+
case rec
|
|
54
|
+
when Resolv::DNS::Resource::IN::A, Resolv::DNS::Resource::IN::AAAA
|
|
55
|
+
rec.address.to_s
|
|
56
|
+
when Resolv::DNS::Resource::IN::MX
|
|
57
|
+
"#{rec.preference} #{rec.exchange}"
|
|
58
|
+
else
|
|
59
|
+
rec.respond_to?(:name) ? rec.name.to_s : rec.to_s
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
# coding: utf-8
|
|
3
|
+
|
|
4
|
+
require "open3"
|
|
5
|
+
require_relative "probe_base"
|
|
6
|
+
|
|
7
|
+
module NetworkInfraUtility
|
|
8
|
+
module Probe
|
|
9
|
+
# ICMP 连通性探测。
|
|
10
|
+
# 复用系统 ping(跨平台),无需 root/raw socket。
|
|
11
|
+
class IcmpProbe < Base
|
|
12
|
+
def self.default_port
|
|
13
|
+
nil
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def call
|
|
17
|
+
out, latency = timing { run_ping }
|
|
18
|
+
if out[:ok]
|
|
19
|
+
rtt = out[:rtt]
|
|
20
|
+
result(:ok, latency: rtt || latency, message: "ICMP 可达", detail: out[:raw])
|
|
21
|
+
else
|
|
22
|
+
result(:fail, latency: latency, message: out[:message], detail: out[:raw])
|
|
23
|
+
end
|
|
24
|
+
rescue StandardError => e
|
|
25
|
+
result(:unknown, message: e.message)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
private
|
|
29
|
+
|
|
30
|
+
def run_ping
|
|
31
|
+
count = @options.fetch(:count, 3)
|
|
32
|
+
cmd = if windows?
|
|
33
|
+
["ping", "-n", count.to_s, "-w", timeout_ms.to_s, @host]
|
|
34
|
+
else
|
|
35
|
+
["ping", "-c", count.to_s, "-W", @timeout.to_i.to_s, @host]
|
|
36
|
+
end
|
|
37
|
+
stdout, stderr, status = Open3.capture3(*cmd)
|
|
38
|
+
output = stdout.to_s.empty? ? stderr.to_s : stdout.to_s
|
|
39
|
+
text = decode_output(output)
|
|
40
|
+
rtt = parse_rtt(text)
|
|
41
|
+
if status.exitstatus == 0
|
|
42
|
+
{ ok: true, rtt: rtt, raw: text }
|
|
43
|
+
else
|
|
44
|
+
{ ok: false, message: "ping 失败 (exit #{status.exitstatus})", raw: text }
|
|
45
|
+
end
|
|
46
|
+
rescue Errno::ENOENT
|
|
47
|
+
{ ok: false, message: "未找到 ping 命令", raw: nil }
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def timeout_ms
|
|
51
|
+
(@timeout * 1000).to_i
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# 将 ping 输出转为合法 UTF-8(Windows 控制台默认 GBK/CP936)。
|
|
55
|
+
def decode_output(raw)
|
|
56
|
+
return nil if raw.nil? || raw.empty?
|
|
57
|
+
enc = windows? ? (Encoding.locale_charmap || "UTF-8") : "UTF-8"
|
|
58
|
+
raw.dup.force_encoding(enc).encode(Encoding::UTF_8, invalid: :replace, undef: :replace)
|
|
59
|
+
rescue StandardError
|
|
60
|
+
raw.dup.force_encoding(Encoding::UTF_8).scrub
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# 解析输出中的 RTT(毫秒),取平均。兼容 time=/时间= 两种关键词。
|
|
64
|
+
def parse_rtt(text)
|
|
65
|
+
return nil unless text
|
|
66
|
+
values = text.scan(/(?:time|时间)\s*[=<]\s*(\d+(?:\.\d+)?)\s*ms/).flatten.map(&:to_f)
|
|
67
|
+
values.empty? ? nil : (values.sum / values.size).round(2)
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
end
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "probe_base"
|
|
4
|
+
|
|
5
|
+
module NetworkInfraUtility
|
|
6
|
+
module Probe
|
|
7
|
+
# NTP 服务探测(UDP 123)。
|
|
8
|
+
# 发送标准 48 字节 NTP 客户端报文,收到响应即判定可达。
|
|
9
|
+
class NtpProbe < Base
|
|
10
|
+
NTP_EPOCH_OFFSET = 2_208_988_800 # 1900-01-01 与 1970-01-01 的秒差
|
|
11
|
+
|
|
12
|
+
def self.default_port
|
|
13
|
+
123
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def call
|
|
17
|
+
packet = build_packet(@options.fetch(:version, 4))
|
|
18
|
+
reply, latency = timing { udp_exchange(packet) }
|
|
19
|
+
if reply == :refused
|
|
20
|
+
result(:refused, message: "UDP 端口拒绝/无监听")
|
|
21
|
+
elsif reply && reply.bytesize >= 48
|
|
22
|
+
result(:ok, latency: latency, message: "NTP 响应正常", detail: ntp_summary(reply))
|
|
23
|
+
else
|
|
24
|
+
result(:timeout, latency: latency, message: "无 NTP 响应")
|
|
25
|
+
end
|
|
26
|
+
rescue Errno::ECONNREFUSED, Errno::ECONNRESET
|
|
27
|
+
result(:refused, message: "UDP 端口拒绝/无监听")
|
|
28
|
+
rescue SocketError => e
|
|
29
|
+
result(:fail, message: "地址解析失败: #{e.message}")
|
|
30
|
+
rescue StandardError => e
|
|
31
|
+
result(:unknown, message: e.message)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
private
|
|
35
|
+
|
|
36
|
+
# 构造 NTP 客户端报文:首字节 = LI(0) + VN(version) + Mode(3)。
|
|
37
|
+
def build_packet(version = 4)
|
|
38
|
+
flags = ((version & 0x07) << 3) | 0x03
|
|
39
|
+
t = Time.now.to_f + NTP_EPOCH_OFFSET
|
|
40
|
+
sec = t.to_i
|
|
41
|
+
frac = ((t - sec) * (1 << 32)).to_i & 0xFFFFFFFF
|
|
42
|
+
[flags].pack("C") + ("\x00" * 39) + [sec, frac].pack("N2")
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def ntp_summary(reply)
|
|
46
|
+
stratum = reply.getbyte(1)
|
|
47
|
+
"stratum=#{stratum}"
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "probe_base"
|
|
4
|
+
|
|
5
|
+
module NetworkInfraUtility
|
|
6
|
+
module Probe
|
|
7
|
+
# RADIUS 服务探测(UDP 1812 认证端口)。
|
|
8
|
+
# 发送 Access-Request,收到任意响应码即判定可达。
|
|
9
|
+
#
|
|
10
|
+
# 注意:RADIUS 服务器会丢弃无法通过共享密钥校验的请求,
|
|
11
|
+
# 因此「无响应」不能等同于「服务不可达」,结果需结合其它信息判断。
|
|
12
|
+
class RadiusProbe < Base
|
|
13
|
+
def self.default_port
|
|
14
|
+
1812
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def call
|
|
18
|
+
packet = build_access_request(@options.fetch(:username, "probe"))
|
|
19
|
+
reply, latency = timing { udp_exchange(packet) }
|
|
20
|
+
if reply == :refused
|
|
21
|
+
result(:refused, message: "UDP 端口拒绝/无监听")
|
|
22
|
+
elsif reply && !reply.empty?
|
|
23
|
+
code = reply.getbyte(0)
|
|
24
|
+
result(:ok, latency: latency, message: "RADIUS 服务可达", detail: "响应码 #{code} (#{code_name(code)})")
|
|
25
|
+
else
|
|
26
|
+
result(:timeout, latency: latency, message: "无响应(RADIUS 可能丢弃无有效共享密钥的请求)")
|
|
27
|
+
end
|
|
28
|
+
rescue Errno::ECONNREFUSED, Errno::ECONNRESET
|
|
29
|
+
result(:refused, message: "UDP 端口拒绝/无监听")
|
|
30
|
+
rescue SocketError => e
|
|
31
|
+
result(:fail, message: "地址解析失败: #{e.message}")
|
|
32
|
+
rescue StandardError => e
|
|
33
|
+
result(:unknown, message: e.message)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
private
|
|
37
|
+
|
|
38
|
+
# 构造 Access-Request:Code=1, Id, Length, Request Authenticator(16B), User-Name 属性。
|
|
39
|
+
# Authenticator 用随机字节占位(无共享密钥时无法计算合法值,仅用于探测)。
|
|
40
|
+
def build_access_request(username)
|
|
41
|
+
code = 1
|
|
42
|
+
id = rand(256)
|
|
43
|
+
authenticator = Random.bytes(16)
|
|
44
|
+
attr = [1, username.bytesize + 2].pack("CC") + username
|
|
45
|
+
length = 20 + attr.bytesize
|
|
46
|
+
[code, id, length].pack("CCn") + authenticator + attr
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def code_name(code)
|
|
50
|
+
{
|
|
51
|
+
2 => "Access-Accept",
|
|
52
|
+
3 => "Access-Reject",
|
|
53
|
+
11 => "Access-Challenge"
|
|
54
|
+
}.fetch(code, "未知")
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "probe_tcp"
|
|
4
|
+
require_relative "probe_icmp"
|
|
5
|
+
require_relative "probe_dns"
|
|
6
|
+
require_relative "probe_ntp"
|
|
7
|
+
require_relative "probe_snmp"
|
|
8
|
+
require_relative "probe_radius"
|
|
9
|
+
|
|
10
|
+
module NetworkInfraUtility
|
|
11
|
+
module Probe
|
|
12
|
+
# 协议名 → 探针类注册表。扩展新协议时调用 Registry.register。
|
|
13
|
+
module Registry
|
|
14
|
+
PROBES = {
|
|
15
|
+
icmp: IcmpProbe,
|
|
16
|
+
telnet: TelnetProbe,
|
|
17
|
+
ssh: SshProbe,
|
|
18
|
+
netconf: NetconfProbe,
|
|
19
|
+
snmp: SnmpProbe,
|
|
20
|
+
twamp: TwampProbe,
|
|
21
|
+
dns: DnsProbe,
|
|
22
|
+
ntp: NtpProbe,
|
|
23
|
+
radius: RadiusProbe
|
|
24
|
+
}.freeze
|
|
25
|
+
|
|
26
|
+
module_function
|
|
27
|
+
|
|
28
|
+
# 全部协议名。
|
|
29
|
+
def protocols
|
|
30
|
+
PROBES.keys
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# 按名字取探针类(符号或字符串均可)。
|
|
34
|
+
def [](name)
|
|
35
|
+
PROBES[name.to_s.downcase.to_sym]
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# 注册自定义探针类。
|
|
39
|
+
def register(klass)
|
|
40
|
+
PROBES[klass.protocol] = klass
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module NetworkInfraUtility
|
|
4
|
+
module Probe
|
|
5
|
+
# 单次探测的统一结果对象。
|
|
6
|
+
#
|
|
7
|
+
# status 取值:
|
|
8
|
+
# :ok 服务连通/正常响应
|
|
9
|
+
# :timeout 超时未响应
|
|
10
|
+
# :refused 连接被拒绝
|
|
11
|
+
# :unreachable 主机/网络不可达
|
|
12
|
+
# :fail 其它失败
|
|
13
|
+
# :unknown 未知错误或无法判定
|
|
14
|
+
class Result
|
|
15
|
+
STATUSES = %i[ok fail timeout refused unreachable unknown].freeze
|
|
16
|
+
|
|
17
|
+
attr_reader :protocol, :host, :port, :status, :latency, :message, :detail
|
|
18
|
+
|
|
19
|
+
def initialize(protocol:, host:, port: nil, status:, latency: nil, message: nil, detail: nil)
|
|
20
|
+
@protocol = protocol.to_sym
|
|
21
|
+
@host = host
|
|
22
|
+
@port = port
|
|
23
|
+
@status = status.to_sym
|
|
24
|
+
@latency = latency
|
|
25
|
+
@message = message
|
|
26
|
+
@detail = detail
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def ok?
|
|
30
|
+
@status == :ok
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def fail?
|
|
34
|
+
@status == :fail
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def timeout?
|
|
38
|
+
@status == :timeout
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def refused?
|
|
42
|
+
@status == :refused
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def unreachable?
|
|
46
|
+
@status == :unreachable
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def success?
|
|
50
|
+
ok?
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# 序列化为 Hash,便于 JSON 输出。
|
|
54
|
+
def to_h
|
|
55
|
+
{
|
|
56
|
+
protocol: @protocol,
|
|
57
|
+
host: @host,
|
|
58
|
+
port: @port,
|
|
59
|
+
status: @status,
|
|
60
|
+
latency: @latency,
|
|
61
|
+
message: @message,
|
|
62
|
+
detail: @detail
|
|
63
|
+
}
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def to_s
|
|
67
|
+
mark = ok? ? "OK" : @status.to_s.upcase
|
|
68
|
+
line = "#{@protocol.to_s.upcase} #{@host}#{@port ? ":#{@port}" : ''} -> #{mark}"
|
|
69
|
+
line += " (#{format('%.3f', @latency)}s)" if @latency
|
|
70
|
+
line += " #{@message}" if @message
|
|
71
|
+
line
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
end
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "probe_base"
|
|
4
|
+
|
|
5
|
+
module NetworkInfraUtility
|
|
6
|
+
module Probe
|
|
7
|
+
# 极简 BER (Basic Encoding Rules) 编码器,仅用于构造 SNMP GET 请求。
|
|
8
|
+
# 不依赖第三方 gem,纯标准库实现。
|
|
9
|
+
module BER
|
|
10
|
+
module_function
|
|
11
|
+
|
|
12
|
+
# 构造 SNMP GET 请求报文。
|
|
13
|
+
# version: "1" (SNMPv1) 或 "2c" (SNMPv2c),默认 "2c"
|
|
14
|
+
# community: 团体名,默认 "public"
|
|
15
|
+
# oid: OID 整数数组,默认 [1,3,6,1,2,1,1,1,0] (sysDescr.0)
|
|
16
|
+
def encode_get_request(version: "2c", community: "public", oid: [1, 3, 6, 1, 2, 1, 1, 1, 0], request_id: 1)
|
|
17
|
+
version_int = version.to_s == "1" ? 0 : 1 # SNMPv1=0, v2c=1
|
|
18
|
+
varbind = encode_tlv(0x30, encode_oid(oid) + encode_null)
|
|
19
|
+
varbind_list = encode_tlv(0x30, varbind)
|
|
20
|
+
pdu_body = encode_integer(request_id) + encode_integer(0) + encode_integer(0) + varbind_list
|
|
21
|
+
pdu = encode_tlv(0xA0, pdu_body) # GET-PDU
|
|
22
|
+
msg_body = encode_integer(version_int) + encode_octet_string(community) + pdu
|
|
23
|
+
encode_tlv(0x30, msg_body)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# TLV:tag + 长度 + 内容
|
|
27
|
+
def encode_tlv(tag, content)
|
|
28
|
+
[tag].pack("C") + encode_length(content.bytesize) + content
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# 长度编码(短/长两种形式)
|
|
32
|
+
def encode_length(len)
|
|
33
|
+
if len < 0x80
|
|
34
|
+
[len].pack("C")
|
|
35
|
+
else
|
|
36
|
+
bytes = []
|
|
37
|
+
while len > 0
|
|
38
|
+
bytes.unshift(len & 0xFF)
|
|
39
|
+
len >>= 8
|
|
40
|
+
end
|
|
41
|
+
[0x80 | bytes.size].pack("C") + bytes.pack("C*")
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# 非负整数
|
|
46
|
+
def encode_integer(n)
|
|
47
|
+
bytes = []
|
|
48
|
+
loop do
|
|
49
|
+
bytes.unshift(n & 0xFF)
|
|
50
|
+
n >>= 8
|
|
51
|
+
break if n == 0
|
|
52
|
+
end
|
|
53
|
+
encode_tlv(0x02, bytes.pack("C*"))
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def encode_octet_string(str)
|
|
57
|
+
encode_tlv(0x04, str.b)
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def encode_null
|
|
61
|
+
encode_tlv(0x05, "".b)
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# OID 编码:前两弧合并为 40*a+b,其后每弧按 base-128。
|
|
65
|
+
def encode_oid(oid)
|
|
66
|
+
first = oid[0] * 40 + oid[1]
|
|
67
|
+
subs = [first] + oid[2..]
|
|
68
|
+
bytes = []
|
|
69
|
+
subs.each do |sub|
|
|
70
|
+
chunks = [sub & 0x7F]
|
|
71
|
+
sub >>= 7
|
|
72
|
+
while sub > 0
|
|
73
|
+
chunks.unshift((sub & 0x7F) | 0x80)
|
|
74
|
+
sub >>= 7
|
|
75
|
+
end
|
|
76
|
+
bytes.concat(chunks)
|
|
77
|
+
end
|
|
78
|
+
encode_tlv(0x06, bytes.pack("C*"))
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
# SNMP 服务探测(UDP 161)。
|
|
83
|
+
# 发送 SNMP GET (sysDescr.0),收到响应即判定可达。
|
|
84
|
+
class SnmpProbe < Base
|
|
85
|
+
def self.default_port
|
|
86
|
+
161
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def call
|
|
90
|
+
community = @options.fetch(:community, "public")
|
|
91
|
+
version = @options.fetch(:version, "2c")
|
|
92
|
+
oid = @options.fetch(:oid, [1, 3, 6, 1, 2, 1, 1, 1, 0])
|
|
93
|
+
packet = BER.encode_get_request(
|
|
94
|
+
version: version, community: community, oid: oid,
|
|
95
|
+
request_id: rand(1..2_147_483_647)
|
|
96
|
+
)
|
|
97
|
+
reply, latency = timing { udp_exchange(packet) }
|
|
98
|
+
if reply == :refused
|
|
99
|
+
result(:refused, message: "UDP 端口拒绝/无监听")
|
|
100
|
+
elsif reply && !reply.empty?
|
|
101
|
+
result(:ok, latency: latency, message: "SNMP 响应正常", detail: "version=#{version} community=#{community}")
|
|
102
|
+
else
|
|
103
|
+
result(:timeout, latency: latency, message: "无 SNMP 响应(community/版本不匹配或防火墙丢弃)")
|
|
104
|
+
end
|
|
105
|
+
rescue Errno::ECONNREFUSED, Errno::ECONNRESET
|
|
106
|
+
result(:refused, message: "UDP 端口拒绝/无监听")
|
|
107
|
+
rescue SocketError => e
|
|
108
|
+
result(:fail, message: "地址解析失败: #{e.message}")
|
|
109
|
+
rescue StandardError => e
|
|
110
|
+
result(:unknown, message: e.message)
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
end
|