首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何配置net-ssh才能通过ssh隧道使用rest-client获取数据?

如何配置net-ssh才能通过ssh隧道使用rest-client获取数据?
EN

Stack Overflow用户
提问于 2019-05-21 06:15:28
回答 1查看 77关注 0票数 0

我们有一个elasticsearch设置,它只能从跳转框中使用。我想设置一个ssh隧道,这样我就可以从我的笔记本电脑或Docker容器中进行查询。当我运行ssh时,无论是直接运行还是通过“系统”运行,隧道都会工作,我的post会获取数据。当我尝试使用net-ssh设置隧道时,我得到了RestClient::Exceptions::ReadTimeout。我不确定net-ssh配置中遗漏了什么。我提供了一个简化的代码示例。

我试着用Cygwin在Windows上运行它,并在一个运行Centos7的Docker容器中运行。

代码语言:javascript
复制
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

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

感谢您对我的net-ssh设置中缺少的内容的任何帮助。

EN

回答 1

Stack Overflow用户

发布于 2019-05-31 04:29:31

我怀疑这和端口转发有关。在该块中,您应该能够访问elasticUrl:9200,因为此时您基本上处于跳转主机上。

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56228836

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档