LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-05-2012, 08:57 PM   #1
learning01
LQ Newbie
 
Registered: Jul 2012
Posts: 10

Rep: Reputation: Disabled
Question 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?
 
Old 07-05-2012, 09:40 PM   #2
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,695

Rep: Reputation: 1698Reputation: 1698Reputation: 1698Reputation: 1698Reputation: 1698Reputation: 1698Reputation: 1698Reputation: 1698Reputation: 1698Reputation: 1698Reputation: 1698
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.
 
1 members found this post helpful.
Old 07-15-2012, 09:35 PM   #3
learning01
LQ Newbie
 
Registered: Jul 2012
Posts: 10

Original Poster
Rep: Reputation: Disabled
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...
 
Old 07-15-2012, 10:23 PM   #4
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,308

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
maybe:


Code:
var=$(ssh REMOTE_HOST remote_script.sh var1 var2 var3)
echo var
change the exits in evo2's above into echos.

Last edited by schneidz; 07-15-2012 at 10:26 PM.
 
Old 07-15-2012, 10:59 PM   #5
learning01
LQ Newbie
 
Registered: Jul 2012
Posts: 10

Original Poster
Rep: Reputation: Disabled
Thumbs up

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
 
Old 07-17-2012, 01:05 AM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 7.7 (?), Centos 8.1
Posts: 18,231

Rep: Reputation: 2708Reputation: 2708Reputation: 2708Reputation: 2708Reputation: 2708Reputation: 2708Reputation: 2708Reputation: 2708Reputation: 2708Reputation: 2708Reputation: 2708
Code:
echo $var
Need leading '$' when reading a var's content.

BTW, no spaces around var assignments
Code:
var=$(...)

# Not
var = $(...)
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP Script Does Not Execute Ukee Linux - Server 5 02-24-2008 08:00 PM
Execute bash script inside PHP creatorrr Programming 11 11-21-2007 04:32 PM
How to Execute PHP Script? notooth Linux - General 2 06-02-2005 05:43 AM
Shutdown the System remotely via PHP-Script? linuxuser2005 Linux - General 2 08-30-2004 11:53 AM
PHP -- How to execute a shell script from PHP using FTP functions?? zoonalex Programming 3 07-29-2004 11:51 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:31 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration