【问题标题】:How to SSH to a jumpoff server and SSH from that jumpoff server in Ruby如何在 Ruby 中通过 SSH 连接到跳转服务器并从该跳转服务器进行 SSH
【发布时间】:2016-03-24 03:26:45
【问题描述】:

我正在寻找一种使用 ruby​​ SSH 连接到跳转服务器并从该服务器 SSH 连接到另一台服务器的方法。跳转服务器是唯一可以访问最终服务器的服务器。我正在尝试 Net::SSH,但我似乎无法从已建立的 SSH 连接中获得 SSH 连接。

irb(main):021:0> require "net/ssh"
=> false
irb(main):022:0> Net::SSH.start("10.10.10.10", "admin") do |ssh|
irb(main):023:1* result = ssh.exec!("mm | grep 'dv ' | awk '{print $1}'").strip
irb(main):024:1> p "Going to try to login to #{result}" 
irb(main):025:1> Net::SSH.start(result, "admin") do |ssh1|
irb(main):026:2* hostname = ssh1.exec!("hostname")
irb(main):027:2> p "In #{hostname}"
irb(main):029:2> end
irb(main):030:1> end
"Going to try to login to vz-int-api02"
SocketError: initialize: name or service not known
    from org/jruby/ext/socket/RubyTCPSocket.java:129:in `initialize'
    from org/jruby/RubyIO.java:1178:in `open'
    from /usr/local/jruby-1.7.9/lib/ruby/gems/shared/gems/net-ssh-2.9.2    /lib/net/ssh/transport/session.rb:70:in `initialize'
    from org/jruby/ext/timeout/Timeout.java:105:in `timeout'
    from /usr/local/jruby-1.7.9/lib/ruby/gems/shared/gems/net-ssh-2.9.2    /lib/net/ssh/transport/session.rb:67:in `initialize'
    from /usr/local/jruby-1.7.9/lib/ruby/gems/shared/gems/net-ssh-2.9.2    /lib/net/ssh.rb:207:in `start'
    from (irb):25:in `evaluate'
    from /usr/local/jruby-1.7.9/lib/ruby/gems/shared/gems/net-ssh-2.9.2    /lib/net/ssh.rb:215:in `start'
    from (irb):22:in `evaluate'
    from org/jruby/RubyKernel.java:1119:in `eval'
    from org/jruby/RubyKernel.java:1519:in `loop'
    from org/jruby/RubyKernel.java:1282:in `catch'
    from org/jruby/RubyKernel.java:1282:in `catch'
    from /usr/local/jruby-1.7.9/bin/irb:13:in `(root)'

我也尝试直接从已建立的 SSH 连接运行 SSH 连接,但似乎无法从会话对象启动新会话:

irb(main):035:1> ssh.start(result, "admin") do |ssh1|

给予:

NoMethodError: undefined method `start' for #<Net::SSH::Connection::Session:0x5b11d0d8>

我可能从错误的角度解决了这个问题,但我找不到任何想要通过 SSH 连接进行 SSH 的人。

【问题讨论】:

  • 顺便说一句,我知道这可以通过 shell 命令 (quizlet.com/44970397/…) 完成,但我想在 ruby​​ 中执行此操作

标签: ruby net-ssh


【解决方案1】:

这是可能的,但您应该使用 SSH 的内置 ProxyCommand 设置来执行此操作。如果你有一个~/.ssh/config 文件,那么Net::SSH 将使用它。例如,它可能看起来像这样:

Host the.protected.box
Hostname 10.10.10.20
ProxyCommand ssh 10.10.10.10 nc %h %p 2> /dev/null

那么你可以说:

Net::SSH.start('the.protected.box', 'admin')

代理将正常工作。

如果您遇到问题,通常在将您的工作翻译成 Ruby 之前使用ssh 命令行工具进行调试会更容易。

如果不想使用~/.ssh/config,可以在代码中设置代理命令:

require 'net/ssh/proxy/command'
proxy = Net::SSH::Proxy::Command.new('ssh 10.10.10.10 nc %h %p 2>/dev/null')
Net::SSH.start('10.10.10.20', 'admin', proxy: proxy)

【讨论】:

  • 谢谢这是我使用的方法。它适用于 ruby​​,但在 jruby 中存在一个问题 (github.com/net-ssh/net-ssh/issues/122),该问题已在 net-ssh 版本 3.0.2 中修复,并且需要 ruby​​ >=2.0
【解决方案2】:

您可以通过 SSH 端口转发来做到这一点。 Net::SSH gateway 为你做这件事,但你自己做很容易。

它基本上有一些看起来像这样的代码:

Net::SSH.start(...) do |ssh|
   ssh.forward(12345, "internal host", 22)
   ssh.loop { true }
end

这会设置隧道,然后您与 locahost 端口 12345 建立新的 SSH 连接。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-17
    • 2019-05-17
    • 2021-01-09
    • 2010-10-02
    相关资源
    最近更新 更多