【问题标题】:How do I configure net-ssh so I can fetch data using rest-client through an ssh tunnel?如何配置 net-ssh 以便我可以通过 ssh 隧道使用 rest-client 获取数据?
【发布时间】:2019-05-31 04:29:30
【问题描述】:

我们有一个弹性搜索设置,只能从跳转框获得。我想建立一个 ssh 隧道,这样我就可以从我的笔记本电脑或 Docker 容器中查询。 当我直接或通过“系统”运行 ssh 时,隧道工作并且我的帖子获取数据。当我尝试使用 net-ssh 设置隧道时,我得到一个 RestClient::Exceptions::ReadTimeout。我不确定 net-ssh 配置中缺少什么。我提供了一个简化的代码示例。

尝试在 Windows 上使用 Cygwin 以及在运行 Centos7 的 Docker 容器中运行它。

require 'json'
require 'net/ssh'
require 'rest-client'

def fetchData
  indexName = "REDACTED"
  url = "http://localhost:9999/#{indexName}/_search?pretty"
  body = '{"size":1, "query":{"match_all":{}}}'
  resp = RestClient.post url, body, :content_type => :json, :accept => :json
  return JSON.parse(resp)
end

begin
  userName   = 'REDACTED'
  privateKey = 'id_rsa'
  jumpBoxUrl = 'REDACTED.com'
  elasticUrl = 'REDACTED.com'

  # this works
  system("ssh -fN -o StrictHostKeyChecking=no -i ~/.ssh/#{privateKey} #{userName}@#{jumpBoxUrl} -p 22 -L 9999:#{elasticUrl}:9200 sleep 10 >> logfile")
  puts fetchData

  # wait for the ssh to time out
  sleep 5

  # Timed out reading data from server (RestClient::Exceptions::ReadTimeout) - WHY?!
  #
  Net::SSH.start(jumpBoxUrl, userName, :port=>22, :forward_agent=>true, :verbose=>:info, :keys=>["~/.ssh/#{privateKey}"]) do |session|
    session.forward.local(9999, elasticUrl, 9200)

    # this works - able to authenticate to the shell box
    puts session.exec!("ls -la")

    # this times out - data is not returned
    puts fetchData
  end
end

我希望帖子在使用 net-ssh 时返回与使用 ssh 时相同的数据。

感谢我在 net-ssh 设置中缺少的任何帮助。

【问题讨论】:

    标签: ruby rest-client ssh-tunnel net-ssh


    【解决方案1】:

    我怀疑它与端口转发有关。在该块内,您应该能够访问elasticUrl:9200,因为此时您基本上是在跳转主机上。

    我的建议是 ssh 进入 jumphost 和 curl API 端点作为测试。如果可行,您应该能够逐字复制该命令并将其传递给session.exec!。最后,如果可行,那么您应该能够更新 fetchData 以向 elasticUrl:9200 发出请求,然后您就可以参加比赛了。

    【讨论】:

    • 感谢您的建议,但不幸的是,这违背了目的。我正在尝试使用 ruby​​,而不是一堆外部程序。如示例代码所示,我已经可以在外部使用“ssh”,并且无需引入 curl。我只想要红宝石(忽略宝石正在做的任何可能需要 C 等的操作)。
    猜你喜欢
    • 1970-01-01
    • 2013-11-17
    • 2018-09-16
    • 2018-01-18
    • 2021-05-24
    • 2016-10-23
    • 2022-12-23
    • 2015-06-15
    • 1970-01-01
    相关资源
    最近更新 更多