LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Execute bash or PHP script remotely (https://www.linuxquestions.org/questions/linux-newbie-8/execute-bash-or-php-script-remotely-4175415153/)

learning01 07-05-2012 08:57 PM

Execute bash or PHP script remotely
 
Hello,

I have a bash/php script on a remote server that takes few line arguments. Both servers are centOS.

So far in my research I have come across this where i could copy the bash script locally and execute it passing it to bash that executes it remotely:

Code:

ssh root@REMOTE_HOST 'bash -s' < local_script.sh var1 var2 var3
The script on the remote server writes a bool to a file - I am thinking I can just wget the file and check the bool value that way

Code:

wget http://remoteserver/bool.txt
Is this a correct approach?

evo2 07-05-2012 09:40 PM

Hi,

I think it would be easier if you modified the script so that its exit value is is either 0 or 1 instead of writing to a file.
Eg.
Code:

#!/bin/sh
echo "Foo"
if [ something bad ] ; then
  exit 1
fi
exit 0


Then copy the script to each of the servers, and run from the local host as follows
Code:

ssh REMOTE_HOST remote_script.sh var1 var2 var3
echo $?

The $? variable will contain the exit value of the script.

HTH,

Evo2.

learning01 07-15-2012 09:35 PM

That did the trick for ssh; Thanks!, - however, I also needed it to work non-interactively; hence now i am using sshpass. But the return value as you suggested did not work - so now I am writing the bool to a file on the remote server and fetching it and then reading it...

schneidz 07-15-2012 10:23 PM

maybe:


Code:

var=$(ssh REMOTE_HOST remote_script.sh var1 var2 var3)
echo var

change the exits in evo2's above into echos.

learning01 07-15-2012 10:59 PM

nice suggestion - but did not work.

var = $(sshpass -p password ssh user@ip-address /home/user/script.sh var1 var2)
echo var

Result (nothing)

so i changed it to ticks


var = `sshpass -p password ssh user@ip-address /home/user/script.sh var1 var2`

Result:
var1 var2 0 0

my script on the server returns 0; the other 0 might be from ssh; and var1/var2 from sshpass

if nothing else - then i guess i will filter out the field

chrism01 07-17-2012 01:05 AM

Code:

echo $var
Need leading '$' when reading a var's content.

BTW, no spaces around var assignments
Code:

var=$(...)

# Not
var = $(...)



All times are GMT -5. The time now is 03:26 PM.