Does there exists a way to copy the file back to my connecting host (so my local machine) using my current open connection on the remote host?
Sure! But we need to dip our toes into the ancient world of serial line connectivity...back when the only thing between you and a remote computer was a copper line, and "ip addresses" weren't a thing that existed (for most people), so there weren't any options for establishing some sort of out-of-band connection to a remote host.
Many of the tools we used back then survived into the modern era, and along the way picked up various networking tricks now that serial connectivity wasn't the only option...but they kept the rest of their feature set, including support for "inline" file transfer protocols (that is, transfer protocols that run over your existing connection, rather than opening a new connection).
Here are two solutions that will do what you want; in both cases you'll need to install software on the local and remote hosts.
Using Kermit
Install kermit on both the local and the remote hosts.
Use kermit
to establish the ssh connection to the remote host:
kermit -EC 'ssh remotehost.example.com'
Use kermit
on the remote host to start a file transfer:
remotehost$ kermit -s somefile.txt
Watch your local Kermit automatically enter receive mode and receive the file. You'll see it show up in your local current working directory.
Using screen + lrzsz
You can do something similar using screen and the lrzsz package, which includes a Zmodem implementation.
Install screen
locally.
Install lrzsz
both locally and on the remote host.
Edit your ~/.screenrc
to include:
zmodem catch
Start a new screen
session and connect to the remote host:
screen -e '' ssh remotehost.example.com
Use the sz
command to transfer a file:
remotehost$ sz somefile.txt
This will bring up an rz
command at the bottom of the screen, just press <return>
.
Watch the file transfer to your local system. As before, you'll find the file in your local current working directory.
Using zssh + lrzsz
Zssh is a wrapper for ssh that understand Zmodem escapes. The process looks almost exactly like the previous section, but you use zssh
instead of screen:
Install lrzsz
on both the local and remote hosts.
Connect to the remote host:
localhost$ zssh remotehost
Initiate a file transfer:
remotehost$ sz somefile.txt
Type CTRL-SPACE
to enter file transfer mode and type rz
(then <return>
).
I'm running on Fedora 36, and everything mentioned here (kermit, screen, lrzsz, zssh) is available as a package; to get "all of the above" you would run:
sudo yum install screen ckermit zssh lrzsz
On other distributions you may need to install things manually.
scp
from the same host you're connected from? That would even allow you to use connection sharing (using theControlMaster
andControlPath
ssh
options, with no need to authenticate again). Or do you absolutely need the connection to be in the other direction? – Kusalananda Oct 05 '22 at 13:21