3rgo
3rgo

Reputation: 3153

cURL to remote server through SSH tunnel

I have to write a PHP script that will be executed on my client's network when finalized. My computer (A) cannot connect to that network, but I have SSH access to a single server (B) on it. My script has to do a cURL request (with certificate and private key) to a web server (C) on a specific port on that network. Another difficulty is that I do not have the IP of the C server, only a URL resolvable only when within the network. But server B and C can communicate between each other

Basically I see 3 steps (but there may be more) :

  1. Open SSH connection from computer A to server B
  2. Send cURL request to server C (https://my.remote.server.domain.com:8444) and store response
  3. Close SSH connection

The thing is, I have no idea how to do that (I'm basically ignorant in all things network related). Anyone has a clue ?

Upvotes: 3

Views: 10281

Answers (1)

Flavio Giobergia
Flavio Giobergia

Reputation: 399

Using Bash:

$ ssh user@ssh_server << EOM
curl http://remote.server/ > /home/user/file
EOM
$ scp user@ssh_server:/home/user/file local_file

This first part connects to your ssh server (ssh_server), executes cURL and saves the file locally (on the ssh server). Then, scp is used to download the file on your local machine.

Creating a temporary file is probably the easiest way of doing this. You could create it in /tmp (and, if you really can't stand having that file there, delete it afterwards using ssh + rm: )

$ ssh user@ssh_server 'rm /tmp/file'

Finally, a dirty (and not recommended) way for not creating files is the following:

$ ssh user@ssh_server << EOM
curl http://remote/server | nc -l 1234 &
exit
$ nc ssh_server 1234 > file

I should probably mention once again that this technique should be avoided at all costs, since it transfers unencrypted data and requires no authentication whatsoever. Also, keep in mind that someone else could connect to the server using that same port (1234) before your command executes, thus retrieving the result for themselves, and leaving your script hanging.

So, one last time, don't use that.

Upvotes: 1

Related Questions