Failure bash script remotely, redirecting output locally

I am trying to run a script remotely (from a bash script), but it is difficult for me to get the output for local redirection for analysis. Running the script is not a problem:

ssh -n -l "$user" "$host" '/home/user/script.sh $params'

However, I cannot capture the output of this script. I tried the following:

results=$(ssh -n -l "$user" "$host" '/home/user/script.sh $params')
results=`ssh -n -l "$user" "$host" '/home/user/script.sh $params'`
ssh -n -l "$user" "$host" '/home/user/script.sh $params' | grep "what I'm looking for"
ssh -n -l "$user" "$host" '/home/user/script.sh $params' > results_file

Any ideas?

+5
source share
5 answers
ssh user@host.com "ls -l" >output

You can even do things like:

ssh user@host.com "cat foo.tar" | tar xvf --

To make things simple, create a pub / private key pair using ssh-keygen. Copy the * .pub key to the remote host in ~ / .ssh / authorized_keys, make sure it's chmod'd 600

Then you can do

ssh -i ~ / .ssh / yourkey user@host.com ... etc.

And he will also not ask for a password. (If your key pair is without password).

+4

, , - . , .

shadyabhi@archlinux /tmp $ cat echo.sh 
#!/bin/bash
echo "Hello WOrld"$1
shadyabhi@archlinux /tmp $ ssh -n -l shadyabhi 127.0.0.1 '/tmp/echo.sh' foo
Hello WOrldfoo
shadyabhi@archlinux /tmp $ ssh -n -l shadyabhi 127.0.0.1 '/tmp/echo.sh' foo > out
shadyabhi@archlinux /tmp $ cat out
Hello WOrldfoo
0

, , ssh -n , , , ( , ssh-agent, , authorized_keys ). , , , ( ).

, script.sh stdin /dev/tty stdout/stderr. ssh -n

0

ssh -n -l "$user" "$host" '/home/user/script.sh $params' > results_file

worked as expected. It only lingered when the output was redirected (and the script would take 5-6 minutes to build) and therefore it was not displayed. Thanks to everyone.

0
source

Your script does not receive any of the parameters and is probably running too long because of this. In addition, anything that comes out (on stdout) can be passed to the next command or redirected to a file, like any other local command. Consider the following:

$ cat ~/bin/ascript.sh 
echo one:$1 two:$2 three:$3

$ params="squid whale shark"
$ ssh localhost  'ascript.sh $params'
one: two: three:

$ ssh localhost  "ascript.sh $params"
one:squid two:whale three:shark
0
source

All Articles