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.

peterh
  • 4,914
  • 13
  • 29
  • 44
gnsr
  • 103
  • 1
  • 1
  • 5

4 Answers4

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.

Khaled
  • 35,688
  • 8
  • 69
  • 98
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).

Mark Griffin
  • 166
  • 3
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.

Jeremy Impson
  • 171
  • 1
  • 2
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

Denyo
  • 1