Is it possible to SCP from a remote to local whilst logged into the remote and without knowing the local's IP address?

33,529

Solution 1

You can use SSH tunneling for this.

Using tunneling you can forward a TCP port either from your local machine to the remote machine, or from the remote machine to your local machine. I use it frequently to forward e.g. SMTP or IMAP ports from a remote machine behind a firewall to my local machine (and then access the services locally, as if they were running locally).

To forward port 22 (SSH) from you local machine to the remote machine try this:

ssh -R12345:localhost:22 yourremoteuser@remotemachine

(Note that localhost refers to the local name of the remote machine)

After running this you should be able to ssh back home using:

ssh -p12345 [email protected]

When using scp, you would do something like (scp has an uppercase P for port forwarding):

scp -P12345 filename 127.0.0.1:/tmp/filename

Port forwarding in the other direction (from remote to local) uses -L instead of -R.

The above commands assume that you are using a terminal ssh client. Graphical clients, like PuTTY for Windows, also support tunneling

Solution 2

Try the following in the command line from the remote machine, you may need to enable port forwarding on your router.

scp <file on remote machine> ${SSH_CLIENT%% *}:<directory on local machine>

Source: Easily scp a file back to the host you're connecting from (commandlinefu.com)

Solution 3

Maybe zssh?

zssh (Zmodem SSH) is a program for interactively transferring files to a remote machine while using the secure shell (ssh). It is intended to be a convenient alternative to scp , allowing to transfer files without having to open another session and re-authenticate oneself.

zssh is an interactive wrapper for ssh

It uses the venerable rz, sz implementations of zmodem file transfer.

Solution 4

Upvoted this question, this is something I would like to do achieve easily as well.

Here is related answer: How do I SCP from remote machine to local machine when I am outside of my home network?

You need to allow access to ssh from outside your network. This is done by forwarding a port on the your broadband router to the lan ip of your server. However there's some security concerns with allowing ssh access from outside, so you may also want to look into methods to secure ssh, particually key-based authentication and disable password authentication entirely.

Share:
33,529

Related videos on Youtube

jnthnclrk
Author by

jnthnclrk

Sunburnt expat living and working on a tropical island.

Updated on September 18, 2022

Comments

  • jnthnclrk
    jnthnclrk almost 2 years

    I regularly find myself wanting to copy a file from remote terminal session to my local machine. Usually I log out of the remote session and call an scp transfer from local to copy the file from remote to local. But this feels a little long winded. I would like to transfer the file whilst logged into the remote over SSH to save time. My local machine is connected to the internet from a dynamic IP range so I'm never quite sure how to connect to it remotely. But surely, as the remote session originates from my laptop, there must be a shortcut in scp to get back to my laptop... Right?

  • yardsale8
    yardsale8 about 13 years
    Doesn't answer OP's question whose IP isn't static.
  • yardsale8
    yardsale8 about 13 years
    Unless I missed something this should fine for the OP to connect from the currently remote machine back to the local. The tunneling will be done through the SSH session. As the SSH connection is setup from the dynamic local machine to the remote it will succeed and will thus be able to setup a listening port on the remote machine that tunnels back to where it came from.
  • Septagram
    Septagram over 12 years
    This will require OP to set up SSH server on his local machine and take care of authentication. So this may be more trouble than it's worth in some use cases.
  • yardsale8
    yardsale8 over 10 years
    Not only this works for dynamic IPs, it also works across firewalls that prohibit ssh connections from remote to local. Very useful answer!
  • yardsale8
    yardsale8 over 10 years
    You can have it even more convenient by setting the remote port forwarding with the ~C escape. Just type <Enter>~C-R 12345:localhost:22<Enter>, and you have your tunnel without leaving your already established SSH session.
  • alex
    alex over 8 years
    but the problem is not copying from remote, the problem is copying to remote, I don't see how suspending shell does anything to address that...
  • bizi
    bizi over 8 years
    To explain what this does for people came across this, ${SSH_CLIENT%% *} output the IP of your local machine. So the whole command open a scp session from remote machine to your local machine for file transfering. This can only be done if you can SSH to your local machine from the remote machine.
  • kralyk
    kralyk over 8 years
    This can be further improved by adding something like export ME="${SSH_CLIENT%% *}" into your shell rc file. You can then simply use scp <file> $ME:<local path>
  • Steen Schütt
    Steen Schütt over 7 years
    Well it works the other way too. The idea is just that you suspend the session instead of exiting it, which makes it at least a bit easier.
  • kap
    kap about 5 years
    Great, this works, however note: If you login via VPN the files will not end up on your machine, but somewhere else.