【问题标题】:Net::SSH.start Timeout connecting to Vagrant host in RubyNet::SSH.start 超时连接到 Ruby 中的 Vagrant 主机
【发布时间】:2017-11-30 04:25:20
【问题描述】:

我有一个 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 主机?

【问题讨论】:

    标签: ruby net-ssh


    【解决方案1】:

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

    如果在带有 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 -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'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-01
      • 1970-01-01
      • 2015-09-17
      • 2013-03-13
      相关资源
      最近更新 更多