简体   繁体   中英

Net::SSH.start Timeout connecting to Vagrant host in Ruby

I have a vagrant VM running.

vagrant init centos/7

Generates the minimal Vagrantfile :

Vagrant.configure(2) do |config|
  config.vm.box = "centos/7"
end

vagrant ssh-config reports the following:

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

However, the following seems to fail:

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"
    ]
})

With the following:

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>'

I can connect using SSH, as expected:

ssh -p 2222 -i /path/to/.vagrant/machines/default/virtualbox/private_key vagrant@127.0.0.1

How can I connect to a vagrant host in Ruby on my local machine?

This isn't an answer but it deserves to be added to the thread in an effort to help ...

If had this exact problem with Chef and Ruby on a local machine with VBox so first know you aren't alone. It does not appear to be a problem with the underlying Ruby framework which you can test with:

Note you'll need to adjust IP & user

#!/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"

Then execute with: ruby ./test.rb

The Chef failure has a very similar output to your own and note the same versions:

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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM