Execut bash script in PHP - php

I need to execut a bash script which mainly contains a pg_dump and some log, clean,... stuff through a PHP file located in the /var/www/ folder
My server is a Debian Squeeze 6.0.6 with PHP5 and Apache2 recently installed.
My index.php folder contains the code below :
<?php
echo shell_exec('./backup_database');
?>
The script seems to work because I got the logs output in my browers but the pg_dump is not executed.
Obviously, if I run the script manually it works.
The script is located in the same folder as index.php and I used chmod 777 on those two files and on the parent directory.
P.S. I have poor knowledges in server configuration, try to be as specific as possible

Try
<?php
exec('./backup_database');
?>
But i think your backup_database Needs an .sh ending so it can be executed. Then it should look like this
<?php
exec('sh ./backup_database.sh');
?>
Or
<?php
exec('./backup_database.sh');
?>
i'm not quite shure which of the two Solutions might work for you

Related

Unable to run Bash Script from PHP Code out of HTML Page

having a small issue here.
Already tried many approaches I found on the web to resolve this, didn't get it to work so far.
I am trying to execute a server-side Linux Bash Script when a Button on a static HTML side is clicked.
It just doesn't work.
My setup is a nginx webserver running on ubuntu, got a static HTML page with PHP code within it:
Ubuntu Version: 18.04.2 LTS (Bionic Beaver) /
Linux Version: 5.4.0-42-generic /
Nginx Version: nginx/1.14.0 (Ubuntu) /
PHP Version: 7.4.11
Bash Script is stored in the same folder as the index.html for the website (/var/www/website).
For testing purposes, gave ownership if the script file to the user www-data and even gave full access to all users to the file (chmod 777 script.sh),
script file is also definitely executalble.
Also this works perfectly fine:
sudo -u www-data php -r "shell_exec('bash script.sh');"
PHP Code within the HTML file:
<?php
shell_exec('bash script.sh)';
?>
Tested with some echo's that this part gets executed. Also looks good.
But the script is never running ..
Would very much appreciate any hints, as you may be able to tell, my web skills are as bad as my scripting skills ;)
Thanks a lot and best regards
Juls
Is the shellscript executable without sudo? Maybe try
chmod +x script.sh

xampp linux run executables with php

My goal is essentially to run executable(say .sh or .py) with with php code, where executable would potentially create some new files in the directory.
For example, an obvious first try (shell_exec):
<?php
$output = shell_exec('sh shell.sh');
echo $output;
?>
Where the shell file is:
#!/bin/bash
# My first script
whoami
mkdir "NewDir"
Does not fully work if executed by visiting a page. whoami command returns "daemon" (not my account name) and folder "NewDir" is not being created.
I think this is permissions problem, even though my htdocs folder has permissions 777. (What I am trying to do works on Windows).
I think if I could make this simple script create a directory then my original would be resolved.
Any suggestions?
Well actually this is quite simple. Get the .py or the .sh file and use:
<?php
Include_once 'filename';
?>
Thank you for your time
Try using the full path in your bash script
#!/bin/bash
# My first script
whoami
mkdir /tmp/testFolder
Make it executable and set permissions to Folder:
chmod +x bashscript.sh
chmod 777 /tmp/testFolder
Test to start it:
./bashscript
Then everything should woring

Stuck at os.rename()

i am ubuntu user.. i have a command in php that exec a python file.. the python file is set to executable.. so, my php command is:-
shell_exec("try.py");
the python file is located at desktop.. but the php file is located in www folder..
/var/www/try.php
in the try.py, i have a code to rename a file on the desktop as follow:-
print "enter"
os.rename("a.txt", "b.txt")
print "exit"
so, the try.py and a.txt are in desktop..
my problem is, when i execute the php file, it shows the "enter" only but not with the "exit".. so i guess it cannot execute the os.rename maybe because of the root privilege or anything that i dont know.. i have tried some solutions to disable password for sudo but still i didnt show the "exit".
but, if i execute the try.py directly by double click it on the desktop and execute it, the command can be done and the output shows:-
enter
exit
so, anyone knows how to execute it using php?
You can directly rename files in php using the rename function.
rename($oldname, $newname);
The problem is when you are doing os.rename("a.txt", "b.txt") it is looking for a.txt in the directory from where the application is running (so it is looking for a.txt in /var/www/.
You should give both a.txt and b.txt the full path:
os.rename('/home/user/Desktop/a.txt', '/home/user/Desktop/b.txt')
You will also have to make sure that www-data (or whatever user is running Apache) can write to the Desktop directory; however from a security perspective this is a very bad idea - any script running on the server can read the contents of your desktop (and even delete the files).
finally, i got the solution..
to be able to rename the file in desktop using os.rename, we need to edit some of the configurations in sudoers..
it involved some steps in visudo and also the php script..
in visudo (using command "sudo visudo" in terminal:)-
#user privilege specification
root All=(ALL:ALL) ALL
www-data ALL=(ALL:ALL) ALL
#includedir /etc/sudoers.d
ubuntu ALL=(ALL) NOPASSWD: ALL //use your username.. mine is ubuntu
www-data ALL=(ALL) NOPASSWD: ALL //the place where i put the php file
in php file:-
<?php
$output = exec("sudo python /home/ubuntu/Desktop/[filename.py]");
?>
for python file is just the same..
os.rename("a.txt", "b.txt")
ok.. that is the solution.. now i can run the python script which it will change the name of a file in desktop from php..
Thank you for all the comments and suggestion for the solutions.. =))
You need something like:
my_dir = os.path.dirname(os.path.realpath(sys.argv[0]))
That will equate to the directory try.py is actually in.
Then you can execute:
os.rename(os.path.join(my_dir, 'a.txt'), os.path.join(my_dir, 'b.txt'))
As mentioned above, this isn't necessarily a great idea from a security PoV though, since you have to grant write permissions to the php script.
I also usually put the my_dir code in an if sys.argv[0]: so that I can still debug from python REPL:
if sys.argv[0]:
# running as executable
my_dir = os.path.dirname(os.path.realpath(sys.argv[0]))
else:
# imported into interpreter
my_dir = '.'
That way I can still import the module into REPL to debug it, as long as I'm in the same directory as the file in question.

php shell_exec() command is not working

I am trying to run a .sh file from php.
I tried doing it with shell_exec(). but its not working
I refered many questions related to this in stack overflow but could not solve
my php code is(web.php)
<?php
echo shell_exec('/var/www/project/xxe.sh');
echo "done";
?>
only done is printed. but it is working from terminal(php /var/www/project/web.php)
In xxe.sh I am calling a python file
python vin.py
I have also changed the file permission to 777 for both .sh n .py files
please help
If you say it works on the terminal and not on apache then apache's php.ini file may be disabling the use of shell_exec().
See http://www.php.net/manual/en/ini.core.php#ini.disable-functions
Your apache's php.ini file may look something like
disable_functions=exec,passthru,shell_exec,system,proc_open,popen
Remove shell_exec from this list and restart the web server, although this is a security risk and I don't recommend it.
shell_exec might not know what directory to look in for your executable's location directory. What solved it for me was this before the shell_exec:
putenv('PATH=/usr/local/bin');
Then the terminal can find the executable. Also check permissions on every part of the command to make sure apache user has read and execute permissions.
If it works well in shell, I think apache is chrooted. So php can't find /var/...
Or user of httpd user does not have permission to enter /var/...
If you are good at PHP. Open dir /var/... And readdir() and check dir exists and check file exists.
This question might help you. scanning /home/ with opendir()
The problem is usually that when you exec code from within php it is run as the webservers user www-data in alot of linux distros. Normaly this user does not have an enviroment set up, and because of that no PATH. By using full paths in your files you can usually overcome this.
xxe.sh
/usr/bin/python /path/to/script/vin.py
While trying to run a script triggered by github post-receive webhook.
Here is where my project directory is located(cloned git repo):
/var/www/html/my-repo
I create a script inside the above directory called webhook.php:
<?php
#webhook.php
$cmd = shell_exec("git pull 2>&1");
#for debugging
echo $cmd;
?>
Execute the following command inside /var/www/html
sudo chown www-data:www-data -R my-repo/
Test it by going to http://www.myserver.com/my-repo/webhook.php
Add the path to your script to github webhooks.
I have been stuck in this problem for several hours.
I have thought about a solution.
1. move your script to a python file "script.py" and place this file to your server root.
2. shell_exec("python script.py");
Any way, it works for me.
On my host I had to give a different path for my php file to be executed from shell_exec().
This didn't work shell_exec('/usr/bin/php backgroundtask.php');.
While this did shell_exec('/opt/php/php-5.5.0/bin/php backgroundtask.php');.
You can visit this Reference.
I had the same issue because PHP backslashes.
PHP escapes the backslashes, so the command that reaches the shell
'COPY E:path1\path2\file.prn /B \127.0.0.1\"PRINTER NAME"'
so I gave command like this
'COPY E:\\path1\\path2\\file.prn /B \\\\127.0.0.1\"PRINTER NAME"'.
You have to double-escape the backslashes: once for PHP and once for the shell.

running php script in filezilla

I am using fileZilla ftp server and trying to run my php scipt. Below code to call b.php is not working. I am new to filezilla so let me know if any configuration required to run this code. I am calling b.php from a.php and both of them are in same folder.
<?php
header("Location: b.php");
?>
It will not work like that. You can run PHP file via terminal:
$ php -f file.php
Or you can make it executable with $ chmod +x file.php, add #!/usr/bin/env php on top of file and, finally, you run like that:
$ ./file.php
Or just access it in browser if you have Web Server configured and running.
This code may help you...
Try to write like this
<?php
header('Location: b.php');
exit();
?>

Categories