Master connection
It's easiest if you plan in advance.
Open a master connection the first time. For subsequent connections, route slave connections through the existing master connection. In your ~/.ssh/config
, set up connection sharing to happen automatically:
ControlMaster auto
ControlPath ~/.ssh/control:%h:%p:%r
If you start an ssh session to the same (user, port, machine) as an existing connection, the second session will be tunneled over the first. Establishing the second connection requires no new authentication and is very fast.
So while you have your active connection, you can quickly:
Forwarding
On an existing connection, you can establish a reverse ssh tunnel. On the ssh command line, create a remote forwarding by passing -R 22042:localhost:22
where 22042 is a randomly chosen number that's different from any other port number on the remote machine. Then ssh -p 22042 localhost
on the remote machine connects you back to the source machine; you can use scp -P 22042 foo localhost:
to copy files.
You can automate this further with RemoteForward 22042 localhost:22
. The problem with this is that if you connect to the same computer with multiple instances of ssh, or if someone else is using the port, you don't get the forwarding.
If you haven't enabled a remote forwarding from the start, you can do it on an existing ssh session. Type Enter ~C
Enter -R 22042:localhost:22
Enter.
See “Escape characters” in the manual for more information.
There is also some interesting information in this Server Fault thread.
Copy-paste
If the file is small, you can type it out and copy-paste from the terminal output. If the file contains non-printable characters, use an encoding such as base64.
remote.example.net$ base64 <myfile
(copy the output)
local.example.net$ base64 -d >myfile
(paste the clipboard contents)
Ctrl+D
More conveniently, if you have X forwarding active, copy the file on the remote machine and paste it locally. You can pipe data in and out of xclip
or xsel
. If you want to preserve the file name and metadata, copy-paste an archive.
remote.example.net$ tar -czf - myfile | xsel
local.example.net$ xsel | tar -xzf -
Copying a file from one host to another requires a daemon on the remote host, implementing some application-level file transmission protocol. This is a requirement no matter from which language you are going to talk to that remote daemon.
Your options for Linux systems are:
- SSH. This requires a SSH daemon (say openssh-server) on the remote side. Because ssh is designed for security you will have to configure the remote host to authenticate you with either a password or a private key. Actually copying the file can be done via the scp utility or ssh client library (jsch would be an example of such).
- NFS. The remote host installs a daemon (for example samba) and shares some files. Your local computer (cifs-utils package is capable of that) can then mount a remote location on the local file system. This way you can copy a file to the remote host by just copying the file locally. Authentication is optional, files are sent in plain over the network.
- FTP. An ftp server is installed on remote side and configured to permit access to certain locations for certain users. You can then use any ftp client or some ftp client library (commons-net library from the Apache project, for instance) to connect to the remote ftp server and copy the files. Authentication is optional, files are sent in plain over the network.
All of this seems like a lot of work, and in deed it is, because there is not a single widely-adopted and standardized protocol that would be implemented and configured out-of-the-box on most systems.
Best Answer
Syntax:
To copy a file from
B
toA
while logged intoB
:To copy a file from
B
toA
while logged intoA
: