LinuxQuestions.org
Help answer threads with 0 replies.
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, 09:57 PM   #1
learning01
LQ Newbie
 
Registered: Jul 2012
Posts: 8

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, 10:40 PM   #2
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
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, 10:35 PM   #3
learning01
LQ Newbie
 
Registered: Jul 2012
Posts: 8

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, 11:23 PM   #4
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,029

Rep: Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845
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 11:26 PM.
 
Old 07-15-2012, 11:59 PM   #5
learning01
LQ Newbie
 
Registered: Jul 2012
Posts: 8

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, 02:05 AM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,241

Rep: Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325
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 09:00 PM
Execute bash script inside PHP creatorrr Programming 11 11-21-2007 05:32 PM
How to Execute PHP Script? notooth Linux - General 2 06-02-2005 06:43 AM
Shutdown the System remotely via PHP-Script? linuxuser2005 Linux - General 2 08-30-2004 12:53 PM
PHP -- How to execute a shell script from PHP using FTP functions?? zoonalex Programming 3 07-29-2004 12:51 PM


All times are GMT -5. The time now is 09:45 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration