Galet
Galet

Reputation: 6299

How to get list of file names from a private remote ip

I am having access to ServerA and don't have access to ServerB.I want to get the list of file names from ServerB via serverA.

I am logging into ServerA using the following command and doing some functions.

 Net::SSH.start(url, user, forward_agent: true) do |ssh|

  ssh.exec('scp -r source dest')
 end

But i want to get the list the file names from ServerB via ServerA. How can i do it ?

Eg: Dir["/path/*.txt"] or ls *.txt

OS: Linux
Language: ruby

Upvotes: 1

Views: 418

Answers (1)

Paulo Fidalgo
Paulo Fidalgo

Reputation: 22311

You can use ssh to execute a remote command:

ssh username@hostname ls -l /foo/bar

If the ls command is not enough you can always use find or any other command.

EDIT

Here you have a full working script

require 'net/ssh'

Net::SSH.start('localhost', 'user', :password => "password") do |ssh|

  stdout = ''
  ssh.exec!("ls -l /tmp") do |channel, stream, data|
    stdout << data if stream == :stdout
  end
  puts stdout
end

working with ruby 2.1.2p95

Also make sure you have ruby compiled with OpenSSL:

ruby -ropenssl -e 'puts OpenSSL::OPENSSL_VERSION'

EDIT 2

What you need is a tunnel, for more information check the official documentation.

require 'net/ssh/gateway'

gateway = Net::SSH::Gateway.new('host', 'user')

gateway.ssh("host.private", "user") do |ssh|
  puts ssh.exec!("hostname")
end

gateway.open("host.private", 80) do |port|
  Net::HTTP.get_print("127.0.0.1", "/path", port)
end

gateway.shutdown!

Upvotes: 2

Related Questions