LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 06-10-2007, 03:16 AM   #1
achilles
LQ Newbie
 
Registered: Apr 2007
Posts: 7

Rep: Reputation: 0
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.
 
Old 06-10-2007, 03:38 AM   #2
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,135

Rep: Reputation: 230Reputation: 230Reputation: 230
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.

Last edited by Guttorm; 06-10-2007 at 05:43 AM.
 
Old 06-10-2007, 07:06 AM   #3
achilles
LQ Newbie
 
Registered: Apr 2007
Posts: 7

Original Poster
Rep: Reputation: 0
Hi Guttorm,
thanks for your help, I'll try it now.
yes, missing ";" at last line
 
Old 06-10-2007, 09:05 AM   #4
achilles
LQ Newbie
 
Registered: Apr 2007
Posts: 7

Original Poster
Rep: Reputation: 0
Hi Guttorm,
it's not work too.
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.
 
Old 06-10-2007, 01:21 PM   #5
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,135

Rep: Reputation: 230Reputation: 230Reputation: 230
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?
 
Old 06-11-2007, 11:15 AM   #6
achilles
LQ Newbie
 
Registered: Apr 2007
Posts: 7

Original Poster
Rep: Reputation: 0
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 !!
 
Old 06-12-2007, 08:33 AM   #7
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,135

Rep: Reputation: 230Reputation: 230Reputation: 230
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.
 
Old 06-12-2007, 12:58 PM   #8
hoodedmanwithsythe
Member
 
Registered: Apr 2006
Location: West Midlands, UK
Distribution: mandriva, centos, debian
Posts: 91

Rep: Reputation: 15
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

Last edited by hoodedmanwithsythe; 06-12-2007 at 01:08 PM.
 
Old 06-13-2007, 05:15 AM   #9
achilles
LQ Newbie
 
Registered: Apr 2007
Posts: 7

Original Poster
Rep: Reputation: 0
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.
 
Old 06-15-2007, 10:48 AM   #10
hoodedmanwithsythe
Member
 
Registered: Apr 2006
Location: West Midlands, UK
Distribution: mandriva, centos, debian
Posts: 91

Rep: Reputation: 15
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

Last edited by hoodedmanwithsythe; 06-15-2007 at 11:02 AM.
 
Old 08-02-2007, 03:44 PM   #11
hoodedmanwithsythe
Member
 
Registered: Apr 2006
Location: West Midlands, UK
Distribution: mandriva, centos, debian
Posts: 91

Rep: Reputation: 15
brother says that you need ssi as the php can't run such programs
 
  


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/shell script to install mysql,apache and php automatically stranger_6_7 Linux - General 2 08-11-2009 02:07 AM
Using PHP, run bash script and see results on Server's Monitor xmrkite Linux - Software 6 10-17-2006 01:28 PM
What must I do when I want to run PHP on Web browser ? manhcuong Linux - Software 1 12-06-2005 03:25 AM
.sh Script to run another via browser GetMooked Linux - Software 1 05-12-2005 04:41 PM
PHP -- How to execute a shell script from PHP using FTP functions?? zoonalex Programming 3 07-29-2004 11:51 AM


All times are GMT -5. The time now is 10:39 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration