1

Considering I have connected via ssh to a remote host and I want to copy a file from that remote host to my local device:

I know that I can just copy that file from a shell on the local device by using something like:

> scp user@remote:/path/to/file ./

and be done with it.

But when I am already on the remote host and I have no idea what my local IP is, is there a way to specify a copy to the local machine to the directory I was when I connected via ssh?

> pwd
/home/user/documents/stuff
> ssh user@remote
Connected to remote host
> scp file.txt REMOTECONNECTION
[==========] 100% Done.
> exit
> ls
file.txt
2
  • 1
    Can we assume that your username is the same on both systems?
    – Jeff Schaller
    Commented Oct 10, 2019 at 14:55
  • No, this is usually not the case.
    – salbeira
    Commented Oct 14, 2019 at 21:23

1 Answer 1

2

Yes, this is in fact possible using the environment variable SSH_CLIENT.

Try this:

scp /path/to/my_file username@$(echo $SSH_CLIENT | awk '{ print $1}'):

Note that this will push the file to your home directory.

1
  • Consider using "${SSH_CONNECTION%% *}" instead of the $( ... ) part.
    – Jeff Schaller
    Commented Oct 10, 2019 at 14:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .