引用net-ssh API,传递来自yaml的连接参数的“最佳”方式是什么?
错误:
thufir@dur:~/ruby$
thufir@dur:~/ruby$ ruby ssh.rb
{:host=>"localhost", :user=>"me", :port=>123, :password=>"mypassword"}
/home/thufir/.rvm/gems/ruby-2.0.0-p247/gems/net-ssh-2.6.8/lib/net/ssh.rb:165:in `start': wrong number of arguments (1 for 2..3) (ArgumentError)
from ssh.rb:14:in `<main>'
thufir@dur:~/ruby$
代码:
#!/usr/bin/env ruby
require 'rubygems'
require 'net/ssh'
require 'yaml'
require 'pry'
conn = YAML.load_file('conn.yml')
params = {:host => conn['host'], :user => conn['user'],
:port => conn['port'],:password => conn['password']}
puts params
Net::SSH.start(params) do |ssh|
puts 'hi'
end
YAML:
user: me
password: mypassword
port: 123
host: localhost
是否可以传递一个整体对象?或者,您是否必须将其分解为主机,用户等。人
答案 0 :(得分:4)
host
和user
参数必须指定为位置参数,而不是options
哈希的一部分。因此:
Net::SSH.start(conn['host'], conn['user'], port: conn['port'], password: conn['password']) do |ssh|
# ...
end
或者您可以直接传递所有YAML键值对:
Net::SSH.start(conn.delete('host'), conn.delete('user'), conn) do |ssh|
# ...
end