How do I refer to my local computer for scp'ing when logged into remote?

Asbestos picture Asbestos · Aug 29, 2013 · Viewed 12.3k times · Source

This must be a really simple question, but I am trying to move a file from a remote server to my local computer, while logged into the remote (via ssh).

All of the guides say to just use

scp name@remote:/path/to/file local/path/to/file

But as far as I can understand, that would be what I would use from my local machine. From the remote machine, I assume that I want to use something like

scp /path/to/file my_local_computer:/local/path/to/file

but (if that's even correct) how do I know what to put in for my_local_computer?

Thanks!

Answer

Andreas picture Andreas · Apr 22, 2014

You can automatically figure out where you're logged in from by checking the environment variables SSH_CONNECTION and/or SSH_CLIENT. SSH_CONNECTION for example shows the client address, the outgoing port on the client, the server address and the incoming port on the server. See section ENVIRONMENT in man ssh

So, if you want to copy a file from the server to the client from which you're logged in from, the following (which infers the client ip by taking the first part of SSH_CONNECTION) should work:

scp /path/to/file $(echo $SSH_CONNECTION | cut -f 1 -d ' '):/local/path/to/file

Andreas