0

I have a shell script (script.sh) on a local linux machine that I want to run on a few remote servers. The script should take a local txt file (output.txt) as an argument and write to it from the remote server.

script.sh:

#!/bin/sh

file="$1"
echo "hello from remote server" >> $file

I have tried the following with no success:

ssh user@host "bash -s" < script.sh /path/to/output.txt
2
  • The script you've included in your question refers to /bin/sh rather than bash on its first line, and does not include anything specific to bash. Do you really require bash for this, or is a portable, POSIX solution what you're after? Commented Apr 6, 2018 at 9:51
  • Edited. Yes you're correct, the solution doesn't necessarily need to use bash Commented Apr 6, 2018 at 9:56

2 Answers 2

4

So if I'm reading this correctly...

  • script.sh is stored on the local machine, but you'd like to execute it on remote machines,
  • output.txt should get the output of the remotely executed script, but should live on the local machine.

As you've written things in your question, you're providing the name of the local output file to a script that won't be run locally. While $1 may be populated with a value, that file it references is nowhere to be seen from the perspective of where it's running.

In order to get a script to run on a remote machine, you have to get it to the remote machine. The standard way to do this would be to copy the script there:

$ scp script.sh user@remotehost:.
$ ssh user@remotehost sh ./script.sh > output.txt

Though, depending on the complexity of the script, you might be able to get away with embedding it:

$ ssh user@remotehost sh -c "$(cat script.sh)" > output.txt

The advantage of this is of course that it doesn't require disk space to be used on the remote machine. But it may be trickier to debug, and the script may function a little differently if it's run in-line like this rather than from a file. (For example, positional options will be different.)


If you REALLY want to provide the local output file as an option to the script you're running remotely, then you need to include a remote path to get to that script. For example:

script.sh:

#!/bin/sh

outputhost="${1%:*}"    # trim off path, leaving host
outputpath="${1#*:}"    # trim off host, leaving path

echo "Hello from $(hostname)." | ssh "$outputhost" "cat >> '$outputpath'"

Then after copying the script over, call the script with:

$ ssh user@remotehost sh ./script.sh localhostname:/path/to/output.txt

That way, script.sh running on the remote host will send its output independently, rather than through your existing SSH connection. Of course, you'll want to set up SSH keys and such to facilitate this extra connection.

Sign up to request clarification or add additional context in comments.

1 Comment

The scp option seems to be the most simple and does the job. Thanks!
0

You can achieve it like below:-

For saving output locally on UNIX/Linux:

   ssh remote_host "bash -s script.sh" > /tmp/output.txt

The first line of your script file should be #!/bin/bash and you don't need to use bash -s in your command line. Lets try like below:-

Better always put full path for your bash file like

 ssh remote_host "/tmp/scriptlocation/script.sh" > /tmp/output.txt

For testing execute any unix command first:-

 ssh remote_host "/usr/bin/ls -l" > /tmp/output.txt

For saving output locally on Windows:,

 ssh remote_host "script.sh" > .\output.txt

Better use plink.exe . Example

plink remote_host "script.sh" > log.txt

3 Comments

Unfortunately I can't use plink as it ins't installed on either machine. ssh remote_host "ls -l" > /tmp/output.txt doesnt work as the script is situated on the local machine and can't be found on the remote machine
@Gary I have provided you for ssh option as well. Try with that.
so rather than "ls -l" i need to execute script.sh

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.