Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
source "http://rubygems.org"
source 'http://rubygems.org'

# Specify your gem's dependencies in net-snmp2.gemspec
gemspec
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Bundler::GemHelper.install_tasks
task :console do
require 'pry'
require 'logger'
$: << 'lib'
$LOAD_PATH << 'lib'
require 'net-snmp2'
Net::SNMP::Debug.logger = Logger.new(STDOUT)
Net::SNMP::Debug.logger.level = Logger::INFO
Expand Down
257 changes: 128 additions & 129 deletions bin/mib2rb
Original file line number Diff line number Diff line change
@@ -1,129 +1,128 @@
#! /usr/bin/env ruby

$: << "#{File.dirname(__FILE__)}/../lib"
require 'logger'
require 'optparse'
require 'erb'
require 'net-snmp2'

Net::SNMP.init
Templates = Net::SNMP::MIB::Templates

usage = <<USAGE

Usage: mib2rb [OPTION]... ROOT_NODE [ERB_FILE]

Description
Prints a mib subtree according to the ERB_FILE.
Within the ERB file, the `root` variable contains the
Net::SNMP::Node object corresponding to ROOT_NODE, and
`nodes` is a collection of the root & all descendants.

Options
-h, Prints this usage information.

Aliases: --help

-l LEVEL Set the log level.
Logs are piped to STDERR, so you can redirect
STDOUT to a file without worrying about logs
getting into the output.

Values: debug, info, warn, error, fatal, none
Default: none
Aliases: --log-level

-f FORMAT Select the output format.
If this option is supplied with an ERB file,
the option is ignored and the file is used instead.

Values: [d]efault, [j]son
Default: default
Aliases: --format

Arguments
ROOT_NODE [Required] The root node of the mib tree to translate.
May be specified as numeric oid or mib name.

ERB_FILE [Optional] The template file to use for output.

Default: Builtin template specifying human readable output.
(See below)

Default ERB_FILE Template
-------------------------

#{Templates::DESCRIBE}
USAGE

root_node = nil
erb_template = nil

OptionParser.new do|opts|
opts.on( '-h', '--help') do
puts usage
exit
end

opts.on('-l', '--log-level LEVEL') do |level|
break if level =~ /none/i

Net::SNMP::Debug.logger = Logger.new(STDERR)
Net::SNMP::Debug.logger.level = case level
when /debug/i
Logger::DEBUG
when /info/i
Logger::INFO
when /warn/i
Logger::WARN
when /error/i
Logger::ERROR
when /fatal/i
Logger::FATAL
else
puts "Invalid log level: #{level}"
puts
puts usage
exit(1)
end
end

opts.on('-f', '--format FORMAT') do |format|
case format
when /^d(efault)?$/i
erb_template = Templates::DESCRIBE
when /^j(son)?$/i
erb_template = Templates::JSON
else
puts "Invalid format: #{format}"
puts
puts usage
exit(1)
end
end

end.parse!

case ARGV.length
when 1
root_node = Net::SNMP::MIB.get_node(ARGV[0])
# If format wasn't set by -f option, default it here
erb_template ||= Templates::DESCRIBE
when 2
root_node = Net::SNMP::MIB.get_node(ARGV[0])
erb_template = File.read(ARGV[1])
else
puts "Invalid arguments..."
puts
puts usage
exit(1)
end

def render(node, erb_template)
root = node
nodes = [root] + root.descendants.to_a
erb = ERB.new(erb_template, nil, '-')
puts erb.result binding
end

render(root_node, erb_template)
#! /usr/bin/env ruby

$LOAD_PATH << "#{File.dirname(__FILE__)}/../lib"
require 'logger'
require 'optparse'
require 'erb'
require 'net-snmp2'

Net::SNMP.init
Templates = Net::SNMP::MIB::Templates

usage = <<USAGE

Usage: mib2rb [OPTION]... ROOT_NODE [ERB_FILE]

Description
Prints a mib subtree according to the ERB_FILE.
Within the ERB file, the `root` variable contains the
Net::SNMP::Node object corresponding to ROOT_NODE, and
`nodes` is a collection of the root & all descendants.

Options
-h, Prints this usage information.

Aliases: --help

-l LEVEL Set the log level.
Logs are piped to STDERR, so you can redirect
STDOUT to a file without worrying about logs
getting into the output.

Values: debug, info, warn, error, fatal, none
Default: none
Aliases: --log-level

-f FORMAT Select the output format.
If this option is supplied with an ERB file,
the option is ignored and the file is used instead.

Values: [d]efault, [j]son
Default: default
Aliases: --format

Arguments
ROOT_NODE [Required] The root node of the mib tree to translate.
May be specified as numeric oid or mib name.

ERB_FILE [Optional] The template file to use for output.

Default: Builtin template specifying human readable output.
(See below)

Default ERB_FILE Template
-------------------------

#{Templates::DESCRIBE}
USAGE

root_node = nil
erb_template = nil

OptionParser.new do |opts|
opts.on('-h', '--help') do
puts usage
exit
end

opts.on('-l', '--log-level LEVEL') do |level|
break if level =~ /none/i

Net::SNMP::Debug.logger = Logger.new(STDERR)
Net::SNMP::Debug.logger.level = case level
when /debug/i
Logger::DEBUG
when /info/i
Logger::INFO
when /warn/i
Logger::WARN
when /error/i
Logger::ERROR
when /fatal/i
Logger::FATAL
else
puts "Invalid log level: #{level}"
puts
puts usage
exit(1)
end
end

opts.on('-f', '--format FORMAT') do |format|
case format
when /^d(efault)?$/i
erb_template = Templates::DESCRIBE
when /^j(son)?$/i
erb_template = Templates::JSON
else
puts "Invalid format: #{format}"
puts
puts usage
exit(1)
end
end
end.parse!

case ARGV.length
when 1
root_node = Net::SNMP::MIB.get_node(ARGV[0])
# If format wasn't set by -f option, default it here
erb_template ||= Templates::DESCRIBE
when 2
root_node = Net::SNMP::MIB.get_node(ARGV[0])
erb_template = File.read(ARGV[1])
else
puts 'Invalid arguments...'
puts
puts usage
exit(1)
end

def render(node, erb_template)
root = node
nodes = [root] + root.descendants.to_a
erb = ERB.new(erb_template, nil, '-')
puts erb.result binding
end

render(root_node, erb_template)
19 changes: 9 additions & 10 deletions bin/net-snmp2
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#! /usr/bin/env ruby

$: << "#{File.dirname(__FILE__)}/../lib"
$LOAD_PATH << "#{File.dirname(__FILE__)}/../lib"
require 'logger'
require 'optparse'
require 'erb'
Expand All @@ -10,18 +10,18 @@ require 'net/snmp/repl/manager_repl'

usage = '
Usage: net-snmp2 [-v VERSION] [-c COMMUNITY] [HOST[:PORT]]
'.sub("\n", "")
'.sub("\n", '')

# Default option values
options = {
:version => '2c',
:community => 'public',
:peername => 'localhost',
:port => '161'
version: '2c',
community: 'public',
peername: 'localhost',
port: '161'
}

OptionParser.new do |opt|
opt.on("-v VERSION") do |version|
opt.on('-v VERSION') do |version|
case version
when /^1$/
options[:version] = '1'
Expand All @@ -36,19 +36,18 @@ OptionParser.new do |opt|
end
end

opt.on("-c COMMUNITY") do |community|
opt.on('-c COMMUNITY') do |community|
options[:community] = community
end

opt.on('-h') do
puts usage
exit
end

end.parse!

session = nil
if ARGV.length > 0
unless ARGV.empty?
peername = ARGV[0]
m = peername.match /^([^:]*)(:([0-9]*))?$/
options[:peername] = m[1]
Expand Down
Loading