开发者

Ruby Net/Telnet use boolean to toggle logging

I am trying to set a boolean variable to toggle the logging path in the Net::Telnet module, ie:

telnetdebug = false
telnetlog = false
telnetlogfile = '/var/log/mcacheMonitor.telnet.log'

xmr = Net::Telnet.new("Host" => host,
                      "Timeout" => 10,
                      "Prompt" => /[#]\z/n,
                      'Waittime'   => 0,
                      'Dump_log' => telnetdebug ? "mcmsDebug.log" : nil,
                      'Output_log' => telnetlog ? telnetlogfile : nil)

BUt this code produces the following error:

C:/Ruby192/lib/ruby/1.9.1/net/telnet.rb:300:in `initialize': can't convert nil into String (TypeError)
        from C:/Ruby192/lib/ruby/1.9.1/net/telnet.rb:300:in `open'
        from C:/Ruby192/lib/ruby/开发者_运维知识库1.9.1/net/telnet.rb:300:in `initialize'
        from mcw.rb:26:in `new'
        from mcw.rb:26:in `<main>'


The telnet documentation is cheating. It says the default is nil, but actually it is not. The hash of arguments is checked if it has a key 'Dump_log'. If it does, the value is used as a filename. So this should work:

telnet_arguments={"Host" => host,
                  "Timeout" => 10,
                  "Prompt" => /[#]\z/n,
                  'Waittime'   => 0}
telnet_arguments['Dump_log'] = "mcmsDebug.log" if telnetdebug
telnet_arguments['Output_log'] = telnetlogfile if telnetlog

xmr = Net::Telnet.new( telnet_arguments )
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜