This repository has been archived by the owner on Dec 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
table-from-snmp
executable file
·135 lines (103 loc) · 4.36 KB
/
table-from-snmp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env ruby
require 'optparse'
require_relative File.join('..', 'lib', 'snmp_table_viewer')
def parse_command_line
options = {
SNMPTableViewer::Formatter::Table => {
transpose: false,
},
snmp: {
common: {
version: 'v3',
host: 'localhost',
port: 161,
},
'v1' => {
community: 'public',
},
'v2' => {
community: 'public',
},
'v3' => {
auth_protocol: :md5,
priv_protocol: :des,
},
},
base_oid: '',
headings: [],
formatter: SNMPTableViewer::Formatter::Table,
}
snmp_version = 'v3'
parser = OptionParser.new do |opts|
opts.banner = "Usage: table-from-snmp [options]"
opts.on('-h', '--help', '-?', 'Prints this help.') do
puts opts
exit
end
opts.on('--headings HEADINGS', Array, 'Headings to use in the table (e.g. "A, Bc, D").') do |v|
options[:headings] += v.map(&:strip)
end
opts.on('--headings-for TYPE', String, "Use headings for this table type (#{SNMPTableViewer::HEADINGS_FOR.keys.join('|')}).") do |v|
options[:headings] += SNMPTableViewer::HEADINGS_FOR[v] || []
end
opts.on('--format FORMAT', String, "How to format the output (#{SNMPTableViewer::FORMATTERS.keys.join('|')}) (default table).") do |v|
options[:formatter] = SNMPTableViewer::FORMATTERS[v.downcase]
end
opts.on('--converter CONVERTER', String, "A converter to run on the data before formatting (#{SNMPTableViewer::CONVERTERS.keys.join('|')}).") do |v|
options[:converter] = SNMPTableViewer::CONVERTERS[v.downcase]
end
opts.separator "\nSNMP common options:"
opts.on('-vVERSION', '--version VERSION', 'SNMP version to use (1|2|3|) (default 3).') do |version|
snmp_version = "v#{version}"
options[:snmp][:common][:version] = snmp_version
end
opts.on('--host HOST', 'The host to connect to.') do |v|
options[:snmp][:common][:host] = v
end
opts.on('p PORT', '--port PORT', Integer, 'SNMP port to connect to (default 161).') do |v|
options[:snmp][:common][:port] = v
end
opts.on('--base-oid OID', 'The oid at the start of the table. Can by dotted numbers or ifTable') do |v|
options[:base_oid] = SNMPTableViewer::BASE_OIDS[v.downcase] || v
end
opts.separator "\nSNMP version 1 and 2 options:"
opts.on('-c COMMUNITY', '--comunity COMMUNITY', 'SNMP comunity (default "public").') do |community|
options[:snmp][snmp_version][:community] = community
end
opts.separator "\nSNMP version 3 options:"
opts.on('-u USER', '--user USER', '--username USER', 'SNMP user.') do |user|
options[:snmp]['v3'][:username] = user
end
opts.on('-l LEVEL', '--security-level LEVEL', 'Security level to use (no_auth|auth_no_priv|auth_priv).') do |level|
options[:snmp]['v3'][:security_level] = level
end
opts.on('-a AUTH_PROTOCOL', '--auth-protocol AUTH_PROTOCOL', 'Authentication protocol to use (MD5|SHA) (default MD5).') do |value|
options[:snmp]['v3'][:auth_protocol] = value.downcase.to_sym
end
opts.on('-A AUTH_PASSWORD', '--auth-password AUTH_PASSWORD', 'Authentication password to use.', String) do |value|
options[:snmp]['v3'][:auth_password] = value
end
opts.on('-x PRIV_PROTOCOL', '--priv-protocol PRIV_PROTOCOL', 'Privacy protocol to use (DES|AES) (default DES).') do |value|
options[:snmp]['v3'][:priv_protocol] = value.downcase.to_sym
end
opts.on('-X PRIV_PASSWORD', '--priv-password PRIV_PASSWORD', 'Privacy password to use.', String) do |value|
options[:snmp]['v3'][:priv_password] = value
end
opts.separator "\nTable formatter options:"
opts.on('--[no-]transpose-table', 'Transpose the output table (swap rows and columns).') do |v|
options[SNMPTableViewer::Formatter::Table][:transpose] = v
end
end # create parser
parser.parse!(ARGV)
options
end
options = parse_command_line
snmp_options = options[:snmp][:common].clone
snmp_options.merge!(options[:snmp][snmp_options[:version]])
data = SNMPTableViewer::Fetcher.from_snmp(base_oid: options[:base_oid], **snmp_options)
converter = options[:converter]
data = converter.convert(data) unless converter.nil?
formatter = options[:formatter]
formatter_options = options[formatter] || {}
formatter = formatter.new(data: data, headings: options[:headings])
puts formatter.output(**formatter_options)