我才刚刚开始学习Ruby,并尝试制作一个简单的脚本以将ssh放入服务器。这是我班上的代码片段:
require 'rubygems' if RUBY_VERSION < '1.9.0'
require 'sensu-plugin/check/cli'
gem 'net-ssh', '= 1.1.4'
require 'net/ssh'
class SshServer < Sensu::Plugin::Check::CLI
option :host,
short: '-h HOST',
description: 'Server to ssh into',
required: true
def run
puts "SSHing #{config[:host]} ..."
Net::SSH.start(config[:host]) do |ssh|
puts ssh.exec!('hostname')
puts "Logging out..."
end
end
我尝试输出config [:host]的值,并对服务器名称和config.get进行硬编码。没运气。我使用的是Ruby 2.0.0版。
这是完整的错误消息。抱歉,无法将其放在代码块中,因为它触发了“代码过多”警告:
检查无法运行:
no implicit conversion of Symbol into Hash, [
"/u/user/.gem/ruby/gems/needle-1.3.0/lib/needle/container.rb:141:in `merge'",
"/u/user/.gem/ruby/gems/needle-1.3.0/lib/needle/container.rb:141:in `register'",
"/u/user/.gem/ruby/gems/needle-1.3.0/lib/needle/definition-context.rb:123:in `method_missing'",
"/u/user/.gem/ruby/gems/net-ssh-1.1.4/lib/net/ssh/transport/ossl/hmac/services.rb:30:in `block (2 levels) in register_services'",
"/u/user/.gem/ruby/gems/net-ssh-1.1.4/lib/net/ssh/transport/ossl/hmac/services.rb:29:in `each'",
"/u/user/.gem/ruby/gems/net-ssh-1.1.4/lib/net/ssh/transport/ossl/hmac/services.rb:29:in `block in register_services'",
"/u/user/.gem/ruby/gems/needle-1.3.0/lib/needle/container.rb:107:in `define'",
"/u/user/.gem/ruby/gems/needle-1.3.0/lib/needle/container.rb:250:in `block in namespace_define'",
"/u/user/.gem/ruby/gems/needle-1.3.0/lib/needle/container.rb:177:in `call'",
"/u/user/.gem/ruby/gems/needle-1.3.0/lib/needle/container.rb:177:in `block in namespace'",
"/u/user/.gem/ruby/gems/needle-1.3.0/lib/needle/lifecycle/singleton.rb:42:in `call'",
"/u/user/.gem/ruby/gems/needle-1.3.0/lib/needle/lifecycle/singleton.rb:42:in `block in call'",
"/u/user/.gem/ruby/gems/needle-1.3.0/lib/needle/lifecycle/singleton.rb:40:in `synchronize'",
"/u/user/.gem/ruby/gems/needle-1.3.0/lib/needle/lifecycle/singleton.rb:40:in `call'",
"/u/user/.gem/ruby/gems/needle-1.3.0/lib/needle/service-point.rb:122:in `instance'",
"/u/user/.gem/ruby/gems/needle-1.3.0/lib/needle/container.rb:308:in `get'",
"/u/user/.gem/ruby/gems/needle-1.3.0/lib/needle/container.rb:251:in `namespace_define'",
"/u/user/.gem/ruby/gems/net-ssh-1.1.4/lib/net/ssh/transport/ossl/hmac/services.rb:26:in `register_services'",
"/u/user/.gem/ruby/gems/needle-1.3.0/lib/needle/container.rb:365:in `require'",
"/u/user/.gem/ruby/gems/needle-1.3.0/lib/needle/definition-context.rb:77:in `require'",
"/u/user/.gem/ruby/gems/net-ssh-1.1.4/lib/net/ssh/transport/ossl/services.rb:34:in `block in register_services'",
"/u/user/.gem/ruby/gems/needle-1.3.0/lib/needle/container.rb:107:in `define'",
"/u/user/.gem/ruby/gems/needle-1.3.0/lib/needle/container.rb:250:in `block in namespace_define'",
"/u/user/.gem/ruby/gems/needle-1.3.0/lib/needle/container.rb:177:in `call'",
"/u/user/.gem/ruby/gems/needle-1.3.0/lib/needle/container.rb:177:in `block in namespace'",
"/u/user/.gem/ruby/gems/needle-1.3.0/lib/needle/lifecycle/singleton.rb:42:in `call'",
"/u/user/.gem/ruby/gems/needle-1.3.0/lib/needle/lifecycle/singleton.rb:42:in `block in call'",
"/u/user/.gem/ruby/gems/needle-1.3.0/lib/needle/lifecycle/singleton.rb:40:in `synchronize'",
"/u/user/.gem/ruby/gems/needle-1.3.0/lib/needle/lifecycle/singleton.rb:40:in `call'",
"/u/user/.gem/ruby/gems/needle-1.3.0/lib/needle/service-point.rb:122:in `instance'",
"/u/user/.gem/ruby/gems/needle-1.3.0/lib/needle/container.rb:308:in `get'",
"/u/user/.gem/ruby/gems/needle-1.3.0/lib/needle/container.rb:251:in `namespace_define'",
"/u/user/.gem/ruby/gems/net-ssh-1.1.4/lib/net/ssh/transport/ossl/services.rb:30:in `register_services'",
"/u/user/.gem/ruby/gems/needle-1.3.0/lib/needle/container.rb:365:in `require'",
"/u/user/.gem/ruby/gems/needle-1.3.0/lib/needle/definition-context.rb:77:in `require'",
"/u/user/.gem/ruby/gems/net-ssh-1.1.4/lib/net/ssh/transport/services.rb:137:in `block in register_services'",
"/u/user/.gem/ruby/gems/needle-1.3.0/lib/needle/container.rb:107:in `define'",
"/u/user/.gem/ruby/gems/needle-1.3.0/lib/needle/container.rb:250:in `block in namespace_define'",
"/u/user/.gem/ruby/gems/needle-1.3.0/lib/needle/container.rb:177:in `call'",
"/u/user/.gem/ruby/gems/needle-1.3.0/lib/needle/container.rb:177:in `block in namespace'",
"/u/user/.gem/ruby/gems/needle-1.3.0/lib/needle/lifecycle/singleton.rb:42:in `call'",
"/u/user/.gem/ruby/gems/needle-1.3.0/lib/needle/lifecycle/singleton.rb:42:in `block in call'",
"/u/user/.gem/ruby/gems/needle-1.3.0/lib/needle/lifecycle/singleton.rb:40:in `synchronize'",
"/u/user/.gem/ruby/gems/needle-1.3.0/lib/needle/lifecycle/singleton.rb:40:in `call'",
"/u/user/.gem/ruby/gems/needle-1.3.0/lib/needle/service-point.rb:122:in `instance'",
"/u/user/.gem/ruby/gems/needle-1.3.0/lib/needle/container.rb:308:in `get'",
"/u/user/.gem/ruby/gems/needle-1.3.0/lib/needle/container.rb:251:in `namespace_define'",
"/u/user/.gem/ruby/gems/net-ssh-1.1.4/lib/net/ssh/transport/services.rb:23:in `register_services'",
"/u/user/.gem/ruby/gems/needle-1.3.0/lib/needle/container.rb:365:in `require'",
"/u/user/.gem/ruby/gems/needle-1.3.0/lib/needle/definition-context.rb:77:in `require'",
"/u/user/.gem/ruby/gems/net-ssh-1.1.4/lib/net/ssh/session.rb:126:in `block in initialize'",
"/u/user/.gem/ruby/gems/needle-1.3.0/lib/needle/container.rb:107:in `define'",
"/u/user/.gem/ruby/gems/net-ssh-1.1.4/lib/net/ssh/session.rb:106:in `initialize'",
"/u/user/.gem/ruby/gems/net-ssh-1.1.4/lib/net/ssh.rb:47:in `new'",
"/u/user/.gem/ruby/gems/net-ssh-1.1.4/lib/net/ssh.rb:47:in `start'", "check-directory-exists.rb:59:in `run'",
"/u/user/.gem/ruby/gems/sensu-plugin-1.4.7/lib/sensu-plugin/cli.rb:58:in `block in <class:CLI>'"
]
答案 0 :(得分:1)
我能够找到与您的红宝石版本兼容的版本。我使用ruby版本对此进行了测试
ruby 2.0.0p648 (2015-12-16 revision 53162) [x86_64-darwin18.2.0]
后跟该版本:
gem install net-ssh --version 3.0.2
我能够使我的代码正常运行,而不会出现您的错误。
问题似乎出在net-ssh
的旧版本中,我尝试使用可以正常工作的SSH服务器进行基本测试,但您的版本遇到了相同的错误,但使用的是最新版本5.2.0
,没有错误。我也使用了这种格式:
def run
puts "SSHing #{config[:host]} ..."
Net::SSH.start( config[:host],
:password=> 'password',
:port=> 22,
:username=> 'username') do |ssh|
puts ssh.exec!('hostname')
puts "Logging out..."
end
end