-3

Wondering if it is possible to write a shell script like this, and if possible, any reference/sample implementation I can refer to? Thanks.

  • Step 1, scp a local file on a local box to a remote box ABC, may need input password or we can hard code password into script possible?
  • Step 2, remote execute a script on box ABC, and leverage the file uploaded in Step 1
  • Step 3, the output of Step 2 (which is on console/stdout) is redirected to local box.

I tried this:

scp ~/Downloads/data/1.dat root@host:/root/data /root/exercise/test /root/data/1.dat
Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
Lin Ma
  • 9,739
  • 32
  • 105
  • 175
  • 1
    http://xyproblem.info/ ? – Karoly Horvath Feb 19 '16 at 18:18
  • @KarolyHorvath, I just want to write a distributed (cross multiple boxes) automated test script. If I need to rephrase my question, please advise. Thanks. – Lin Ma Feb 19 '16 at 18:44
  • 1
    nah, I was just wondering whether you're looking for a configuration management tool. – Karoly Horvath Feb 19 '16 at 21:48
  • 1
    Possible duplicate of [how to use ssh to run shell script on a remote machine?](http://stackoverflow.com/questions/305035/how-to-use-ssh-to-run-shell-script-on-a-remote-machine) – Joao Vitorino Feb 19 '16 at 22:53
  • @JoaoVitorino, thanks for the great reference. Tried the method but it does not fully work for me and your advice is appreciated. The confusion is, I need to execute scp first which should be executed locally and then run command "test" on remote box. How to distinguish between local/remote in my scenario? Thanks. Here is the code `scp ~/Downloads/data/1.dat root@host:/root/data /root/exercise/test /root/data/1.dat` – Lin Ma Feb 20 '16 at 00:29
  • @KarolyHorvath, thanks for the advice. I just need simple script in my case to reduce overhead. I just my further issues above and your advice is appreciated. Thanks. :) – Lin Ma Feb 20 '16 at 00:30

2 Answers2

1

You can execute commands using ssh, for example:

$ ssh user@host ls -la

will connect to host host as user, and after successful authorization execute ls -la command, presenting the output locally. After command finishes connection will be closed.

nsilent22
  • 2,763
  • 10
  • 14
  • Thanks nsilent22, I can change "ls -la" to a shell script? – Lin Ma Feb 20 '16 at 00:18
  • Thanks nsilent22 , tried your method but it does not work for me and your advice is appreciated. The confusion is, I need to execute scp first which should be executed locally and then run command "test" on remote box. How to distinguish between local/remote in my scenario? Thanks. Here is the code `scp ~/Downloads/data/1.dat root@host:/root/data /root/exercise/test /root/data/1.dat` – Lin Ma Feb 20 '16 at 00:28
  • 1
    @LinMa: Just do `ssh root@host /root/exercise/test /root/data/1.dat` after you `scp` your file. Of course - make sure `test` has proper execution permissions. – nsilent22 Feb 20 '16 at 10:49
1

I understand that you want to copy a file to a remote machine, run a command there with that file as an argument and get the output on your local machine. Apparently, you need the test program, which is on the remote machine.

Your try takes you halfway there. You could do it as follows:

scp ~/Downloads/data/1.dat root@host:/root/data
ssh root@host '/root/exercise/test /root/data/1.dat'

The first command copies your file, the second runs the command on the remote machine. Depending on the test command, you can new get some output file back to your local machine:

scp root@host:/root/results/outputfile .

Or, if the command writes to standard out, you could redirect the output to a file on the remote machine by appending > /root/results/outputfile to the ssh command and then scp it back to your local machine.

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116