Net :: SSH.start超时连接到Ruby中的Vagrant主机
问题描述:
我有一个vagrant
VM正在运行。Net :: SSH.start超时连接到Ruby中的Vagrant主机
vagrant init centos/7
产生最小Vagrantfile
:
Vagrant.configure(2) do |config|
config.vm.box = "centos/7"
end
vagrant ssh-config
报告如下:
Host default
HostName 127.0.0.1
User vagrant
Port 2222
UserKnownHostsFile /dev/null
StrictHostKeyChecking no
PasswordAuthentication no
IdentityFile "/path/to/.vagrant/machines/default/virtualbox/private_key"
IdentitiesOnly yes
LogLevel FATAL
但是,下面好像失败:
require 'net/ssh'
Net::SSH.start("127.0.0.1", "vagrant", {
:auth_methods => [
"publickey",
"password"
],
:port=>"2222",
:keys => [
"/path/to/.vagrant/machines/default/virtualbox/private_key"
]
})
w ^第i个以下:
Net::SSH::ConnectionTimeout: Net::SSH::ConnectionTimeout
from /usr/local/lib/ruby/gems/2.4.0/gems/net-ssh-4.1.0/lib/net/ssh/transport/session.rb:90:in `rescue in initialize'
from /usr/local/lib/ruby/gems/2.4.0/gems/net-ssh-4.1.0/lib/net/ssh/transport/session.rb:57:in `initialize'
from /usr/local/lib/ruby/gems/2.4.0/gems/net-ssh-4.1.0/lib/net/ssh.rb:233:in `new'
from /usr/local/lib/ruby/gems/2.4.0/gems/net-ssh-4.1.0/lib/net/ssh.rb:233:in `start'
from (irb):2
from /usr/local/bin/irb:11:in `<main>'
我可以使用SSH连接,符合市场预期:
ssh -p 2222 -i /path/to/.vagrant/machines/default/virtualbox/private_key [email protected]
我怎样才能连接到在Ruby中vagrant
主机我的本地机器上?
答
这不是一个答案,但它值得被添加到线程,努力帮助...
如果有这个确切的问题Chef
和Ruby
本地机器上的VBox所以首先知道你并不孤单。它似乎并不与基础Ruby
框架的问题,你可以测试:
注意你需要调整IP &用户
#!/usr/bin/env ruby
require 'net/ssh'
puts "opening connection.\n"
new_connection = Net::SSH.start('192.168.1.116', 'root', {:keys => ['~/.ssh/id_rsa'], :keepalive => true, :keepalive_interval => 60, :timeout => 60})
puts "connection established, run uptime.\n"
puts new_connection.exec!('uptime')
puts "running uname -a\n"
puts new_connection.exec!('uname -a')
puts "sleeping for 300 seconds.\n"
(1..5).each do |iterator|
sleep_seconds = iterator * 60
sleep 60
puts "#{sleep_seconds}\n"
end
puts "running uptime.\n"
puts new_connection.exec!('uptime')
puts "running uname -a\n"
puts new_connection.exec!('uname -a')
puts "closing connection.\n"
new_connection.close
puts "done.\n"
然后执行:ruby ./test.rb
的Chef
失败有一个非常类似的输出到你自己的和注意相同的版本:
DEBUG: establishing connection to chef-arch-node:22
/opt/chefdk/embedded/lib/ruby/gems/2.4.0/gems/net-ssh-4.1.0/lib/net/ssh/transport/session.rb:90:in `rescue in initialize': Net::SSH::ConnectionTimeout (Net::SSH::ConnectionTimeout)
from /opt/chefdk/embedded/lib/ruby/gems/2.4.0/gems/net-ssh-4.1.0/lib/net/ssh/transport/session.rb:57:in `initialize'
from /opt/chefdk/embedded/lib/ruby/gems/2.4.0/gems/net-ssh-4.1.0/lib/net/ssh.rb:233:in `new'
from /opt/chefdk/embedded/lib/ruby/gems/2.4.0/gems/net-ssh-4.1.0/lib/net/ssh.rb:233:in `start'
from /opt/chefdk/embedded/lib/ruby/gems/2.4.0/gems/net-ssh-multi-1.2.1/lib/net/ssh/multi/server.rb:186:in `new_session'
from /opt/chefdk/embedded/lib/ruby/gems/2.4.0/gems/net-ssh-multi-1.2.1/lib/net/ssh/multi/session.rb:488:in `next_session'
from /opt/chefdk/embedded/lib/ruby/gems/2.4.0/gems/net-ssh-multi-1.2.1/lib/net/ssh/multi/server.rb:138:in `session'
from /opt/chefdk/embedded/lib/ruby/gems/2.4.0/gems/net-ssh-multi-1.2.1/lib/net/ssh/multi/session_actions.rb:36:in `block (2 levels) in sessions'
from /opt/chefdk/embedded/lib/ruby/gems/2.4.0/gems/logging-2.2.2/lib/logging/diagnostic_context.rb:474:in `block in create_with_logging_context'