10

The problem is this: I want to copy files from a remote machine.

I am looking for a way to write a shell script that would connect to the remote machine, get the location of the stored file, once I get the location disconnect from the remote server and use scp from within the local machine with the obtained location to copy the file.

Specifically, I want to know how from a script, I would connect to a remote machine, do tasks on that machine. Disconnect, and continue execution locally.

15

Solution 1:

remote_output="$(ssh user@host "remote command")"
scp "user@$host:$remote_output" /local/path

This works by connecting to the remote machine, running some commands, and copying the output to a variable on your local machine. Say, echoing the file path remotely, capturing it locally.

Solution 2:

ssh user@host "determine_path; cat filename" >local_filename

This works by finding and outputting the file directly from remote, and redirecting it to a file locally. This skips scp. This requires that the steps prior to "cat" don't have output (which you can solve with &>/dev/null).

  • 1
    +1 for showing how to solve the whole problem with one connection, instead of separate ssh + scp. – Peter Cordes Aug 29 '17 at 1:55
  • All answers are helpful but this one addresses my problem most specifically – gnsr Aug 29 '17 at 13:38
20

You can write a script in the following format:

command1
command2
...
ssh user@machine command3
...
command4
command5

You need to setup SSH keys to get command3 executed without prompting you for a password.

  • 2
    it should be noted that this will by synchronous. While ssh user@remote command3 is running, command4 can't start – aaaaa says reinstate Monica Aug 28 '17 at 19:33
  • 3
    @aaaaaa unless you did ssh user@machine command3 & – Isaac Aug 28 '17 at 21:53
  • 3
    @Isaac yes, my point is that either can be desireable behavior, but it should be concious decision – aaaaa says reinstate Monica Aug 29 '17 at 3:28
2

I assume you already have some idea of how you'll identify the file that you want to copy, and that you are able to, or already have, written a script or set of commands that will run on the remote machine that does this identification.

Furthermore I'll also assume that those commands print on standard output the full path to the file, or the relative path to the file, relative to the home directory of the SSH user being used for the copy. In my example, the name of the script is "getfilepath.sh", although it doesn't have to be a shell script, it could be some other executable or even a series of commands.

# Script to be executed on local machine.

# put any leading local commands here.

FILE=$(ssh user@emote getfilepath.sh);
scp user@remotehost:"$FILE" .

# put any trailing local commands here.

In the above code, the "$()" notation is a way of capturing the standard output of a command as a shell variable. It is fairly standard, but if you are using a shell that does not understand it, you can instead surround the command with "back-ticks", which are the accent marks to the left of the 1 key on most US keyboards.

0

maybe it is possible to trigger a scheduled Task on the remote machine that does all the work.

This would prevent you from beeing connected to the remote machine while you are searching the location of the files you want to copy.

Im doing this:

comand line triggers a scheduled task which executes a batchscript in my case.

schtasks /run /s remoteserver /u %user% /p %password% /tn "scheduledtaskname"

Best regards

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.