We have an elasticsearch setup that is only available from a jump box. I want to set up an ssh tunnel so I can query from my laptop or a Docker container. When I run ssh, either directly or via 'system' the tunnel works and my post fetches data. When I try to set up the tunnel using net-ssh I get a RestClient::Exceptions::ReadTimeout. I'm not sure what I'm missing in the net-ssh configuration. I've provided a simplified code example.
Tried running this on Windows with Cygwin, and in a Docker container running Centos7.
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
I expect the post to return the same data when using net-ssh as it does when using ssh.
Appreciate any help with what I'm missing in my net-ssh setup.