Quantcast
Channel: Download a file over an active SSH session - Ask Ubuntu
Viewing all articles
Browse latest Browse all 13

Answer by user6702 for Download a file over an active SSH session

$
0
0

I came up with a way to do this with standard ssh. It's a script that duplicates the current ssh connection, finds your working directory on the remote machine and copies back the file you specify to the local machine. It needs 2 very small scripts (1 remote, 1 local) and 2 lines in your ssh config. The steps are as follows:

  1. Add these 2 lines to your ~/.ssh/config:

    ControlMaster auto
    ControlPath ~/.ssh/socket-%r@%h:%p
    

    Now if you have an ssh connection to machineX open, you wont need passwords to open another one.

  2. Make a 1-line script on the remote machine called ~/.grabCat.sh

    \#!/bin/bash<br>
    cat "$(pwdx $(pgrep -u $(whoami) bash) | grep -o '/.*' | tail -n 1)"/$1
    
  3. Make a script on the local machine called ~/.grab.sh

    \#!/bin/bash
    [ -n "$3" ] && dir="$3" || dir="."
    ssh "$1" ".grabCat.sh $2" > "$dir/$2"
    
  4. and make an alias for grab.sh in (~/.bashrc or wherever):

    alias grab=~/.grab.sh
    

That's it, all done. Now if you're logged in to machineX:/some/directory, just fire up a new terminal and type

grab machineX filename

That puts the file in your current working directory on the local machine. You can specify a different location as a third argument to "grab".

Note: Obviously both scripts must be "executable", ie chmod u+x filename.


Viewing all articles
Browse latest Browse all 13

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>