2017-08-15 130 views
0

我有一個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主機我的本地機器上?

回答

0

這不是一個答案,但它值得被添加到線程,努力幫助...

如果有這個確切的問題ChefRuby本地機器上的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' 
相關問題