Net::SSH.start Timeout 连接到 Ruby 中的 Vagrant 主机



我有一个正在运行的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"
]
})

具有以下功能:

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 vagrant@127.0.0.1

如何连接到本地计算机上 Ruby 中的vagrant主机?

这不是一个答案,但它值得添加到线程中以帮助...

如果在带有 VBox 的本地计算机上ChefRuby有确切的问题,请首先知道您并不孤单。底层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 -an"
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 -an"
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'

最新更新