Ruby on Rails SNMP gem - no response from device - ruby-on-rails

I'm using the SNMP gem in a Rails app to control a Cisco switch. The "get" features of the Gem work, and I can retrieve values from the devices, but the "set" feature isn't working so well...
From the command line, the SNMP commands work properly:
bash-4.1# snmpset -v2c -c private-string 192.168.3.1 .1.3.6.1.2.1.2.2.1.7.11 i 2
IF-MIB::ifAdminStatus.11 = INTEGER: down(2)
bash-4.1#
But when I try to set the same OID using the Gem, I get "no response from 192.168.3.1"
In both the actual app and in IRB:
bash-4.1# irb
2.0.0-p0 :001 > require 'snmp'
=> true
2.0.0-p0 :002 > include SNMP
=> Object
2.0.0-p0 :003 > SNMP::Manager.open(:host => "192.168.3.1", :community => "private-string") do |manager|
2.0.0-p0 :004 > varbind = VarBind.new("1.3.6.1.2.1.2.2.1.7.11", "2")
2.0.0-p0 :005?> manager.set(varbind)
2.0.0-p0 :006?> end
SNMP::RequestTimeout: host 192.168.3.1 not responding
from /usr/local/rvm/gems/ruby-2.0.0-p0/gems/snmp-1.1.1/lib/snmp/manager.rb:293:in `set'
from (irb):5:in `block in irb_binding'
from /usr/local/rvm/gems/ruby-2.0.0-p0/gems/snmp-1.1.1/lib/snmp/manager.rb:205:in `open'
from (irb):3
from /usr/local/rvm/rubies/ruby-2.0.0-p0/bin/irb:16:in `<main>'
2.0.0-p0 :007 > exit
I can run the snmpset from the command line on the same machine, and it works correctly, but not using the gem.

Try varbind = VarBind.new("1.3.6.1.2.1.2.2.1.7.11", SNMP::Integer.new(2)) so that the varbind type is correct.

Related

Rails 5: how to change log level in rails console?

I'm using Rails 5.2.5 and I want to change the log level when I use rails console.
I want to stress that I don't want to change any file in the project, but just to do the change from the console.
I saw some suggestions to run:
conf.log_level = :debug
in the console, but I got the error
NameError (undefined local variable or method `config' for
main:Object) Did you mean? conf
I also tried to execute
conf.log_level = :debug
but I also got the error:
Traceback (most recent call last):
2: from (irb):11
1: from (irb):11:in `rescue in irb_binding'
Do you have any suggestion?
Change YourApp configuration (YourApplocated at config/environment/[environment].rb
YourApp.configure do
config.log_level = :info
end
Or you can use Rails.application.configure
2.5.8 :005 > Rails.application.configure do
2.5.8 :006 > puts(config.log_level)
2.5.8 :007?> end
debug
=> nil
2.5.8 :008 > Rails.application.configure do
2.5.8 :009 > config.log_level = :info
2.5.8 :010?> end
=> :info
2.5.8 :011 > Rails.application.configure do
2.5.8 :012 > puts(config.log_level)
2.5.8 :013?> end
info

Ruby and MongoDB: require 'uri' returns false, should return true

I'm trying to connect to my remote mongoDB database through irb shell.
I ran the following:
2.3.0 :001 > require 'mongo'
=> true
2.3.0 :002 > Mongo::Logger.logger.level = ::Logger::INFO
=> 1
2.3.0 :003 > require 'uri'
=> false
The last statement should return true. I attempted to initialise the client after this via:
db = Mongo::Client.new(ENV['MLAB_URI'])
And got the error:
NoMethodError: undefined method `each' for nil:NilClass
I think you haven't set the ENV variable .
check ENV['MLAB_URI'] variable value.

Error using Log4r

I have a problem using Log4r.
uninitialized constant Log4r::Logger::RootLogger
I tried this, but still got an error:
>> require "log4r"
=> true
>> Log4r::DEBUG
NameError: uninitialized constant Log4r::DEBUG
>> Log4r::Logger.root
=> uninitialized constant Log4r::Logger::RootLogger
from /var/lib/gems/1.9.1/gems/log4r-1.1.11/lib/log4r/staticlogger.rb:5:in `root'
from (irb):5
from /usr/bin/irb:12:in `<main>'
Your problem with Log4r::Logger.root is version depending (the actual version 1.1.11 has this problem).
You may use the previous log4r-version 1.1.10:
gem 'log4r', '<=1.1.10' #or '= 1.1.10'
require 'log4r'
Log4r::Logger.root
log4r defines the constants like Log4r::DEBUG with the creation of the first logger.
You need a Log4r::Logger.new('dummy') before you have access to the level constants.
require 'log4r'
p defined? Log4r::INFO #false
Log4r::Logger.new('dummy')
p defined? Log4r::INFO #constant -> is defined
Some background:
There is a constant Log4r::Log4rConfig::LogLevels defining the different levels. The level-constants are defined, when the first logger is created. You may define them also with Log4r.define_levels(*Log4r::Log4rConfig::LogLevels)
This technique allows it to create loggers with different logging levels.
AlthoughLog4r::Logger.root no longer enables the constants, the code included later on in the answer you referred to does introduce the constants:
Log4r.define_levels(*Log4r::Log4rConfig::LogLevels)
per the following:
MacbookAir1:so1 palfvin$ irb
2.0.0p247 :001 > require 'log4r'
=> true
2.0.0p247 :002 > Log4r.define_levels(*Log4r::Log4rConfig::LogLevels)
=> 5
2.0.0p247 :003 > Log4r::DEBUG
=> 1
2.0.0p247 :004 >

remove diacritics with iconv in ruby on rails not working

i have some problem with removing diacritics with iconv in ruby on rails
here is my code:
class Diacritics
def removeDiacritics(text)
dRemover = Iconv.new("ASCII//TRANSLIT", "UTF-8")
text = dRemover.iconv(text).gsub(/[^a-zA-Z0-9 ]/, '')
end
end
this is output:
1.9.3-p392 :001 > require "diacritics"
/usr/local/rvm/gems/ruby-1.9.3-p392#persoc/gems/activesupport-3.2.12/lib/active_support/dependencies.rb:251:in `block in require': iconv will be deprecated in the future, use String#encode instead.
=> true
1.9.3-p392 :002 > remover = Diacritics.new
=> #<Diacritics:0x00000004237068>
1.9.3-p392 :003 > text = "Dánský prezídent"
=> "Dánský prezídent"
1.9.3-p392 :004 > remover.removeDiacritics(text)
=> "Dnsk prezdent"
i expect "Dansky prezident"
server apache on fedora (httpd), using rvm and ruby 1.9.3-p392
Can anybody help me?
You can use the ActiveSupport::Inflector.transliterate method.
ActiveSupport::Inflector.transliterate("Dánsky prezídent") # => "Dansky prezident"
If you need this for a url slug, it's even easier.
"Dánsky prezídent".parameterize # => "dansky-prezident"

Rails 3.2 Obj.try() failing

I just upgraded from Rails 3.1.0 to 3.2.3. Once I got my gem dependency issues cleared up I managed to run tests and I discovered this:
1.9.2p180 :005 > Rails.version
=> "3.2.3"
1.9.2p180 :006 > a = 'test'
=> "test"
1.9.2p180 :007 > a.try(:banana)
NoMethodError: undefined method `banana' for "test":String
from /Users/jamesthullbery/.rvm/gems/ruby-1.9.2-p180#app_tier/gems/activesupport-3.2.3/lib /active_support/core_ext/object/try.rb:32:in `try'
from (irb):7
from /Users/jamesthullbery/.rvm/gems/ruby-1.9.2-p180#app_tier/gems/railties-3.2.3/lib/rails /commands/console.rb:47:in `start'
from /Users/jamesthullbery/.rvm/gems/ruby-1.9.2-p180#app_tier/gems/railties-3.2.3/lib/rails/commands/console.rb:8:in `start'
from /Users/jamesthullbery/.rvm/gems/ruby-1.9.2-p180#app_tier/gems/railties-3.2.3/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
1.9.2p180 :008 >
When I perform the same command on Rails 3.1.0 I get no issues:
1.9.2p180 :004 > Rails.version
=> "3.1.0"
1.9.2p180 :005 > a = 'test'
=> "test"
1.9.2p180 :006 > a.try(:banana)
=> nil
1.9.2p180 :007 >
Has anyone seen this issue? Certainly there is something wrong with my environment and not the Rails framework. It seems to be pretty difficult to accurately search for the method try, so I haven't found anything online yet. Is anyone else seeing this? Thanks!
Object#try is supposed to deal with nil receivers. It should not be used to avoid NoMethodError exceptions.
The behavior you saw in 3.1.0 was a bug and it was "fixed" 7 months ago (as of this writing).

Resources