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

时间:2017-08-15 03:02:26

标签: ruby net-ssh

我正在运行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主机?

1 个答案:

答案 0 :(得分:0)

这不是一个答案,但它应该被添加到帖子中以帮助......

如果在使用VBox的本地计算机上遇到def _build_url(list_here): pulls_url = "http://test.com" + '/{0}/{1}/{2}/{3}'.format(list_here[0],list_here[1],list_here[2],list_here[3]) print pulls_url #prints http://test.com/a/b/c/d _build_url(["a","b","c","d] Chef这个确切的问题,那么首先要知道你并不孤单。对于您可以测试的基础Ruby框架,它似乎不是问题:

请注意,您需要调整IP&amp;用户

Ruby

然后执行:#!/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