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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
Due to network maintenance being performed by our provider, LQ will be down starting at 05:01 AM UTC. The exact duration of the downtime isn't currently known. We apologize for the inconvenience.
|
 |
06-10-2007, 03:16 AM
|
#1
|
|
LQ Newbie
Registered: Apr 2007
Posts: 7
Rep:
|
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.
|
|
|
|
06-10-2007, 03:38 AM
|
#2
|
|
Senior Member
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,007
Rep: 
|
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.
|
|
|
|
06-10-2007, 07:06 AM
|
#3
|
|
LQ Newbie
Registered: Apr 2007
Posts: 7
Original Poster
Rep:
|
Hi Guttorm,
thanks for your help, I'll try it now.
yes, missing ";" at last line
|
|
|
|
06-10-2007, 09:05 AM
|
#4
|
|
LQ Newbie
Registered: Apr 2007
Posts: 7
Original Poster
Rep:
|
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.
|
|
|
|
06-10-2007, 01:21 PM
|
#5
|
|
Senior Member
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,007
Rep: 
|
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?
|
|
|
|
06-11-2007, 11:15 AM
|
#6
|
|
LQ Newbie
Registered: Apr 2007
Posts: 7
Original Poster
Rep:
|
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 !!
|
|
|
|
06-12-2007, 08:33 AM
|
#7
|
|
Senior Member
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,007
Rep: 
|
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.
|
|
|
|
06-12-2007, 12:58 PM
|
#8
|
|
Member
Registered: Apr 2006
Location: West Midlands, UK
Distribution: mandriva, centos, debian
Posts: 91
Rep:
|
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.
|
|
|
|
06-13-2007, 05:15 AM
|
#9
|
|
LQ Newbie
Registered: Apr 2007
Posts: 7
Original Poster
Rep:
|
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.
|
|
|
|
06-15-2007, 10:48 AM
|
#10
|
|
Member
Registered: Apr 2006
Location: West Midlands, UK
Distribution: mandriva, centos, debian
Posts: 91
Rep:
|
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.
|
|
|
|
08-02-2007, 03:44 PM
|
#11
|
|
Member
Registered: Apr 2006
Location: West Midlands, UK
Distribution: mandriva, centos, debian
Posts: 91
Rep:
|
brother says that you need ssi as the php can't run such programs
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 04:40 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|