Convenient method to pull files from a server in an SSH session

aliasfile-transferssh

I often SSH into a cluster node for work and after processing want to pull several results back to my local machine for analysis. Typically, to do this I use a local shell to scp from the server, but this requires a lot of path manipulation. I'd prefer to use a syntax like interactive FTP and just 'pull' files from the server to my local pwd.

Another possible solution might be to have some way to automatically set up my client computer as an ssh alias so that something like

scp results home:~/results

would work as expected.

Is there any obscure SSH trick that'll do this for me?


Working from grawity's answer, a complete solution in config files is something like

local .ssh/config:

Host ex
    HostName ssh.example.com
    RemoteForward 10101 localhost:22

ssh.example.com .ssh/config:

Host home
    HostName localhost
    Port 10101

which lets me do commands exactly like

scp results home:

transferring the file results to my home machine.

Best Answer

I'd prefer to use a syntax like interactive FTP and just 'pull' files from the server to my local pwd.

Use sftp.

(Hint: If you cannot or do not want to open a new window, you can suspend the SSH connection by pressing Enter, ~, CtrlZ, transfer your files, and resume SSH with fg.)

Another possible solution might be to have some way to automatically set up my client computer as an ssh alias

There are a few ways.

  • Static alias for ssh and scp – add this to your ~/.ssh/config:

    Host home
        # If you don't have a domain name, check out DynDNS...
        # ...or just use your IP address.
        Hostname homepc.somedomain.tld
    
        # If necessary, uncomment:
        #User john
        #Port 1234
    

    Use:

    scp foo home:
    

    (If you don't provide a path after :, the file will go to your home directory.)

  • Use the SSH_CLIENT environment variable.

    export client=${SSH_CLIENT%% *}
    if [[ $client == *:* ]]; then
        client="[$client]"
    fi
    

    Put that in ~/.profile or ~/.bash_profile or wherever you want, then use $client when you need your own address.

    scp foo "$client:"
    
  • Mix both.

    ~/.ssh/config:

    Host client
        ProxyCommand ~/bin/reverse-connect %p
    

    ~/bin/reverse-connect:

    #!/bin/sh
    if [[ $SSH_CLIENT ]]; then
        client=${SSH_CLIENT%% *}
    else
        echo "I don't know your address." >&2
        exit 1
    fi
    if [[ $client == *:* ]]; then
        client="[$client]"
    fi
    
    port=${1:-22}
    
    exec socat - "tcp:$client:$port"
    

    Use:

    scp foo client:
    
Related Question