Not really a programming question, but relevant to many programmers...
Let's say I have opened an SSH session to another computer.
remote:html avalys$ ls
welcome.msg index.html readme.txt
remote:html avalys$
Is there any command that I can type in my remote shell that will immediately transfer one of the files in the current directory (e.g. welcome.msg) to my local computer, i.e.
remote:html avalys$ stransfer welcome.msg
Fetching /home/avalys/html/welcome.msg to welcome.msg
/home/avalys/html/welcome.msg 100% 23KB 23.3KB/s 00:00
remote:html avalys$
The only way I know of to do this is to open a parallel SFTP session and CD to my current directory in the SSH session, which is a real PITA when administering a server remotely.
EDIT: I am aware of the possibility of using a reverse sftp/scp connection, but that involves more typing. It would be great if I could type just the name of some command (e.g. "stransfer"), and the file(s) I want transferred, and have it Just Work.
Here is my preferred solution to this problem [as given on a duplicate question I asked]. Set up a reverse ssh tunnel upon creating the ssh session. This is made easy by two bash function: grabfrom() needs to be defined on the local host, while grab() should be defined on the remote host. You can add any other ssh variables you use (e.g. -X or -Y) as you see fit.
Usage:
Positives:
Negatives:
Future work: This is still pretty kludgy. Obviously, it would be possible to handle the authentication issue by setting up ssh keys appropriately and it's even easier to allow the specification of a remote directory by adding a parameter to grab()
More difficult is addressing the other negatives. It would be nice to pick a dynamic port but as far as I can tell there is no elegant way to pass that port to the shell on the remote host; As best as I can tell, OpenSSH doesn't allow you to set arbitrary environment variables on the remote host and bash can't take environment variables from a command line argument. Even if you could pick a dynamic port, there is no way to ensure it isn't used on the remote host without connecting first.
If you are into patching things (that IMHO shouldn't be patched), take a look at ssh-xfer
I had the same thought that Paul Tomblin wrote in a comment to the question. Old terminal sessions used to use x-, y-, and z-modem protocols and tools (
sz
andrz
for the z-modem variants) to achieve something like this. I'm not sure if these will work over a ssh session, but it might be worth a try.Fink supplies a
lrzsz
package with these tool on Mac OS X.Making this a community wiki because I'd feel bad for getting rep after Paul got there first...
In researching this I found a program that works as a drop-in replacement for the openssh client, zssh.
Works like a charm.
You can mount an ssh connection with sshfs. That way you can transfer files within the same ssh connection, while using whatever ssh client you like best. You can even set it up to automount if you like.
You could write a bash script named stransfer that would take a filename argument, then it would interject the filename in the scp command, assuming the server and path to files on server don't change.
Or if the file is always the same you could create an alias in your ~/.bashrc file.
alias getwelcome='scp avalys@remotehost:/home/avalys/html/welcome.msg .'