LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   run shell script bash from browser with php (http://www.linuxquestions.org/questions/programming-9/run-shell-script-bash-from-browser-with-php-560594/)

achilles 06-10-2007 03:16 AM

run shell script bash from browser with php
 
Hi all,
I use php in the first time & I don't know it more.
May I ask you a question ?
how do I run shell bash script in my php file. my shell.php here :

<?php
$res = exec('./myshell.sh');
if ($res != 0){ echo "my shell is false"; }
echo "my shell is true"
?>

But, when I put shell.php in my web server & run via browser. any thing will happen.
Please, tell me how to run shell script via web browser with php.
thank you.

Guttorm 06-10-2007 03:38 AM

Hi

It looks fine to me. But you say "any thing will happen" - what does that mean? The php script will look for a script "myshell.sh" in the current directory - meaning the same as the php script. Is it there?

Also, the script will run as the user of the web-server - typically "www-data". Does it have the permissions it needs to run the bash script? To check, you can go to the directory with the script and type
sudo -u www-data ./myshell.sh

Does it work?

Edit:
Also a ; is missing after last line.

achilles 06-10-2007 07:06 AM

Hi Guttorm,
thanks for your help, I'll try it now.
yes, missing ";" at last line

achilles 06-10-2007 09:05 AM

Hi Guttorm,
it's not work too. :D
I place them in the same directory, chmod & chown this file the same with my apache web server.

I have variable is temp using query mysql database. I want my webserver run the script when $temp != "". When my script run, is ftp file to other host. My code here :
<?php
...
if ($temp != "") {
$cmd = "./ftptransfer";
system($cmd, $exit);
if ($exit != 0) { die($cmd . " exit status " . $exit); }
}
...
?>

When I run this php file on web browser, it not transfer file for me. Please, tell me how to fix it or another way to make this action.I use Linux 2.4.20, apache 2.x.x, mysql 5.x.x & php 5.x.x.

Thanks for your idea.

Guttorm 06-10-2007 01:21 PM

Hi again

It sounds like it's a permission problem. What happens if you try the sudo? Do you get any error message? If you are getting a file with the ftp transfer, does the web-server have write permissions?

achilles 06-11-2007 11:15 AM

Thanks Guttorm,
I've been set permission for my script file, & I run command "sudo -u apache ./ftptransfer". It's good & don't have any error message. it's right ?

My web browser not have any error message too. I also put a shell command into script for copy file & it's running good (for test the script is running).
I can show you that I want my web server make these thing for me :

I have two host, once run webserver with mySQL & php file here(IP: 192.168.1.1). Another host(192.168.1.2)use to store database file with SFTP server running.

That is all thing I want; when my database have change from web browser (after edit --> press submit), the webserver must work two thing : input new data to my database (it's OK); and dump database to file(example file : update.sql --> it's OK). After that it must transfer this file (update.sql) to host 192.168.1.2 via sftp for storage (<<-- It's not working in this step).

My code here :
----------------
file tranfer.php
----------------
<?php
...
<put data to mysql >
<dump database to file >
...
if ($temp != "") {
$cmd = "./ftptransfer";
system($cmd, $exit);
if ($exit != 0) { die($cmd . " exit status " . $exit); }
}
...
?>
-----------------------------
file ftptransfer (shell bash)
-----------------------------
#!/bin/bash

cp -f /usr/local/dump/update.sql /tmp/
## I put it for test script is running --> copy OK. it's no need for me

/usr/bin/psftp -b command.bat -l ftpuser -pw 123 192.168.1.2 > /dev/null 2>&1

exit 0

-----------------------
file command.bat
-----------------------
put /usr/local/dump/update.sql /opt/database_store/
bye

--------------------------------------------------------------------------
I have run file ftptransfer at console shell, it's OK.
I just use php at first time, Please me again !!!
You can tell me what & where is my fault, or how I can check & see error message.
Thank you very much !!

Guttorm 06-12-2007 08:33 AM

Hi

Strange indeed - it should have worked :)

Only thing I can think of:
/usr/bin/psftp -b command.bat -l ftpuser -pw 123 192.168.1.2 > /dev/null 2>&1

Here you suppress any error messages. Maybe remove "> /dev/null" to see if you get something?

I am sorry, but I never used SFTP - I can't help you much there. But PHP has functions you can use so you might not need to use the shell at all. See:

http://www.php.net/manual/en/function.ssh2-sftp.php

You will need to install she ssh2 module first:

http://www.php.net/manual/en/ref.ssh2.php

Hope this helps.

hoodedmanwithsythe 06-12-2007 12:58 PM

why do all that rubbish(not intentional flame but it is there is no need for it all)
try changing this
Code:

$res = exec('./myshell.sh');
with this
Code:

$res = exec('bash | ./myshell.sh');
geeze has no one else on this forum ever done any active php injection to a web page to find files that are not officially available. this is the key part after identifying that they are apache you just use the search bar to tell you what else is in the folder or even the drive by putting the following into the search bar
Code:

'; exec('bash | find')'
and it tells you everything from the current directory up the tree
or to get it to tell you EVERY thing on the drive you just add an extra section of string so it looks like this instead
Code:

'; exec('bash | cd / | find')
there are of course solutions for the problem but I haven't reaserched it as I haven't come across a site that i needed the info on the fix for.
hope this helps

achilles 06-13-2007 05:15 AM

thanks all,
I try to install ssh2 & libssh2 follow your introduction. All install step is OK.
But I restart my apache I found problem within error_log of apache. record logs here :

[Wed Jun 13 15:43:18 2007] [notice] caught SIGTERM, shutting down
[Wed Jun 13 15:43:27 2007] [warn] Init: Session Cache is not configured [hint: SSLSessionCache]
[Wed Jun 13 15:43:27 2007] [notice] Digest: generating secret for digest authentication ...
[Wed Jun 13 15:43:27 2007] [notice] Digest: done
PHP Warning: PHP Startup: Unable to load dynamic library '/usr/local/httpd/modules/ssh2.so' - /usr/local/httpd/modules/ssh2.so: und
efined symbol: core_globals in Unknown on line 0
[Wed Jun 13 15:43:28 2007] [notice] Apache/2.2.4 (Unix) mod_ssl/2.2.4 OpenSSL/0.9.8e DAV/2 PHP/5.2.1 configured -- resuming normal o
perations

Maybe I have problem with my extension_dir in php.ini file or where I have problem ??
I install php in /usr/local/ & apache in /usr/local/httpd.
thanks for any help .
Regards.

hoodedmanwithsythe 06-15-2007 10:48 AM

Quote:

Originally Posted by achilles
thanks all,
I try to install ssh2 & libssh2 follow your introduction. All install step is OK.
But I restart my apache I found problem within error_log of apache. record logs here :

[Wed Jun 13 15:43:18 2007] [notice] caught SIGTERM, shutting down
[Wed Jun 13 15:43:27 2007] [warn] Init: Session Cache is not configured [hint: SSLSessionCache]
[Wed Jun 13 15:43:27 2007] [notice] Digest: generating secret for digest authentication ...
[Wed Jun 13 15:43:27 2007] [notice] Digest: done
PHP Warning: PHP Startup: Unable to load dynamic library '/usr/local/httpd/modules/ssh2.so' - /usr/local/httpd/modules/ssh2.so: und
efined symbol: core_globals in Unknown on line 0
[Wed Jun 13 15:43:28 2007] [notice] Apache/2.2.4 (Unix) mod_ssl/2.2.4 OpenSSL/0.9.8e DAV/2 PHP/5.2.1 configured -- resuming normal o
perations

Maybe I have problem with my extension_dir in php.ini file or where I have problem ??
I install php in /usr/local/ & apache in /usr/local/httpd.
thanks for any help .
Regards.

No Idea about this but I am Talking with my brother (a website designer)about the main problem it seems that this way of doing things that I told you about only works within the search bar (or equivilent) of php scripts so I will send you a better way of doing things by tommorrow I hope.

Note:
the ssh program is a separate operator in my experience and it runs on a separate port I have not seen it embedded in a script before, it is usualy run as follows
Code:

www.myserver.com:22
which would open something like this

hoodedmanwithsythe 08-02-2007 03:44 PM

brother says that you need ssi as the php can't run such programs


All times are GMT -5. The time now is 08:25 PM.