Sender and receiver to transfer files over ssh on request?
I found a solution from another angle. Since version 3.9, OpenSSH supports session multiplexing: a single connection can carry multiple login or file transfer sessions. This avoids the set-up cost per connection.
For the case of the question, I can first open a connection with sets up a control master (-M
) with a socket (-S
) in a specific location. I don't need a session (-N
).
ssh user@host -M -S /tmp/%r@%h:%p -N
Next, I can invoke scp
for each file and instruct it to use the same socket:
scp -o 'ControlPath /tmp/%r@%h:%p' <file> user@host:<remotefile>
This command starts copying almost instantaneously!
You can also use the control socket for normal ssh connections, which will then open immediately:
ssh user@host -S /tmp/%r@%h:%p
If the control socket is no longer available (e.g. because you killed the master), this falls back to a normal connection. More information is available in this article.
It might work to use sftp instead of scp, and to place it into batch mode. Make the batch command file a pipe or UNIX domain socket and feed commands to it as you want them executed.
Security on this might be a little tricky at the client end.
Have you tried sshfs
?You could:
sshfs remote_user@remote_host:/remote_dir /mnt/local_dir
Where
/remote_dir
was the directory you want to send files to on the system you are sshing into/mnt/local_dir
was the local mount location
With this setup you can just cp
a file into the local_dir
and it would be sent over sftp
to remote_host
in its remote_dir
Note that there is a single connection, so there is little in the way of overhead
You may need to use the flag -o ServerAliveInterval=15
to maintain an indefinite connection
You will need to have fuse
installed locally and an SSH server supporting (and configured for) sftp