Execute bash code from HTML to running docker command - php

I have a bash code like this:
sudo docker exec -it container_id /bin/bash -c 'cp test1.txt test2.txt'
rm test3.txt
It worked well in terminal. But I want to call it from html. My html code is :
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>run bash</title>
</head>
<body>
<?php
if(isset($_POST['submit']))
{
$output=shell_exec('sh /Users/path/script.sh');
}
?>
<p><?php echo $output; ?></p>
</body>
Basically, I want to click the button "submit" (the php code I put above will be called by another html file) and it will execute the bash code to login to docker and run the command cp test1.txt test2.txt.
But it only deleted test3.txt while it didn't copy the file on docker when I ran it from html. Also, I didn't get any output from website. This was weird cause it could work in terminal just by sh /Users/path/script.sh.
I am new to both html and docker. Will be really appreciate it if anyone could give me advice.

Since you are executing docker with "sudo" you'd need the command to be executed via shell_exec() with sudo as well.
Furthermore, if your server environment uses chroot, the docker binary might not be even available to the user running the PHP process.
I suggest checking with your system administrator.
/off As this is not a programming question, you might have better luck approaching people in Server Fault

First of all you need to allowed ( probably www-data user ) to use sudo without password for this one script.
You can do this via sudoers:
vim /etc/sudoers
Add line:
<user> ALL=(ALL) NOPASSWD: /path/to/your/script.sh
Then your shell_exec should looks like this:
$output=shell_exec('/usr/bin/sudo /path/to/your/script.sh');
But be careful, use sudo only if know what you are doing ;)
I also suggest to print output using "pre"
<pre><?php echo $output; ?></pre>
Good luck !

In my case, the error was not about sudo.
To get the stdout, I ran this
<pre><?php echo exec('sh /path/to/my/script/script.sh 2>&1', $output, $return_var); ?></pre>
<pre><?php print_r($output); ?></pre>
<pre><?php echo $return_var; ?></pre>
Noted that when an error occurs during the execution, or the process does not produce output, shell_exec() will return NULL. Therefore, using function shell_exec() cannot detect whether the process is successfully executed by the return value. If you need to check the exit code executed by the process, use the exec() function instead.
It outputed the input device is not a TTY.
And in my bash script, I changed sudo docker exec -it container_id /bin/bash -c 'cp test1.txt test2.txt' into docker exec -i container_id /bin/bash -c 'cp test1.txt test2.txt'.
Basically, ranning docker exec -i rather than docker exec -it fixed my problem. Hope this will help guys have same problem.

Related

Cannot Call Python script from PHP

I am trying to call a python script from PHP but not having any luck. I have searched for hours but found nothing. The python script is running just fine when I call it from the command line(connected to a relay switch, just runs through them, turning them on and off) and it works just fine. However, I can't seem to figure out how to get it to run from PHP. I am very new to PHP but here is what I am using:
<!doctype html>
<head>
<meta charset="UTF-8"/>
</head>
<?php
if(isset($_POST['switch'])){
exec("sudo python /home/pi/Desktop/test.py");
}
?>
<form method="post">
<button name="switch">Switch</button>
</form>
</html>
What am I doing wrong? I can't seem to find an answer anywhere that will make it work. The PHP is displaying the button just fine, but it does nothing when I click it.
shell_exec — Execute command via shell and return the complete output as a string . reference
<?php
if(isset($_POST['switch'])){
$c=escapeshellcmd("sudo python /home/pi/Desktop/test.py");
$res=shell_exec($c);
echo $res; // returns result to display
}
?>
in your script,output is not printed that may seem to not working
<?php
if(isset($_POST['switch'])){
$s=exec("sudo python /home/pi/Desktop/test.py");
echo "$s";
}
?>
add full path of interpreter in the first line of python script . if you have installed more than one python version
$s=exec("sudo -u /home/pi/Desktop/test.py"); this gives permission to python file
first of all make python file executable with chmod +x /path/to/python-script.py
EDIT:
from this post
You can't use sudo from a PHP script. Apache is running from an user (www-data generaly), so edit this file : /etc/sudoers
Then add this line :
www-data ALL=(ALL) NOPASSWD:ALL
Care ! this will authorize all functions to be called by a PHP script, you can adapt changing "ALL" by your script or Python command.
Then precise your user in your exec command :
<?php
exec('sudo -u www-data python /usr/lib/cgi-bin/script.py')
Please make sure that the www user has the permission to execute your python script.
and then you should check if the system could find the PATH of the python libraries that you import in your python code.
I have the same experience with you, and I fixed the problem by checking the apache2 error_log, you'd better try, The error_log will tell what the real problem is !
cd /var/log/apache2
sudo more error.log
chmod 777 test.php
chmod 777 test.py
Good luck!

How do I allow www-data user to execute bash script with nginx

I an Ubuntu 16.04 machine running NGINX and PHP. I would like to enable the www-data user (via web browser) to be able to access a PHP page (php-test.php) that will execute either a bash script (script_test.sh) or execute Linux CLI commands using shell_exec or exec.
I have done the following.
Created my bash script file script_test.sh
#!/bin/bash
whoami
echo $USER
echo 'test'
exit
when I run this from CLI, using
./ script_test.sh
It does indeed work and I can see the info echoed out in the CLI.
I then pursued the goal of being able to allow the www-data user run this bash script through a PHP page running on this same machine from NGINX.
I created my php page (php_test.php) and it contains the following
<?php
chdir('/path/to/my/files/');
shell_exec('./script_test.sh'); // ATTEMPT RUN SCRIPT
shell_exec('/path/to/my/files/script_test.sh'); // ATTEMPT RUN SCRIPT
echo 'test 123'; // SIMPLE ECHO IN THE PHP PAGE
?>
I then ran the following to modify the sudoers file, giving www-data access to the bash script
sudo nano /etc/sudoers
to which I added the following line
www-data ALL=NOPASSWD: /path/to/my/files/script_test.sh
I then made sure the script was executable, for the sake of my testing, not worrying about security, I just set it to 777 with the following command
sudo chmod 777 script_test.sh
From there I opened a web browser and browsed to the localhost (NGINX) web server (php_test.php) and the only thing I see on the page is the 'test 123' that I echo from PHP... none of the bash script appears to have run at all. I tailed the NGINX error log and don't see any error at all.
Is there another log that could contain clues on this?
What else should I check here?
The result of shell_exec() is returned as string. To display it in your browser, simply add echo.
<?php
chdir('/path/to/my/files/');
echo shell_exec('./script_test.sh'); // ATTEMPT RUN SCRIPT
echo shell_exec('/path/to/my/files/script_test.sh'); // ATTEMPT RUN SCRIPT
echo 'test 123'; // SIMPLE ECHO IN THE PHP PAGE
?>
See the Return Values in the manual:
The output from the executed command or NULL if an error occurred or
the command produces no output.
Can you try to use passthru instead of shell_exec, and see the output anything?
Also try this, and see if it shows on the log file:
if(file_exists('/path/to/my/files/script_test.sh')) { die('File not found!'); }
shell_exec("nohup /path/to/my/files/script_test.sh > /path/to/my/files/output.log &");
Also, are you running PHP with the www-data user (check your fpm pool)?
Do you have any error on /var/log/syslog or /var/log/auth.log ?
Have you restarted the server after changing the sudo permissions?
What does su - www-data -c "whoami" and su - www-data -s /bin/bash -c "whoami" outputs?
Does su - www-data -s /bin/bash -c "/path/to/my/files/script_test.sh" output something?

execute shell command with php

im trying to execute a bash script with a php/html button to wake my nas.
<form action="" method="POST">
<input type="submit" value="Wake NAS" name="zero" />
</form>
<?php
if (isset($_POST["zero"])){
#echo "Hello World!";
shell_exec("/var/www/html/wakenas.sh &");
}?>
"Hello World" is printed when button is pressed.
but code won't be executed.
the wakenas.sh looks like this and works if i execute it over shell
#!/bin/bash
etherwake -D "BC:5F:F4:09:E1:07"
echo "why!?!?!" > "/var/www/html/works.txt"
exit 1
wakenas.sh has all rights
Maybe you guys know why it wont be executed.
thanks in advance
The easy and secure way of executing your script is to put in sudoers. Assuming your Linux distribution is Debian base and user of who run the web server is www-data, then you can create a file e.g /etc/sudoers.d/wakeup_ether
Cmnd_Alias WAKE_UP_CMD = /var/www/html/wakenas.sh
www-data ALL=NOPASSWD: WAKE_UP_CMD
Modify your script to prefix the command with sudo.
shell_exec("sudo /var/www/html/wakenas.sh &");
Reference: https://help.ubuntu.com/community/RootSudo
From your dump:
etherwake: This program must be run as root.
when you execute wakenas.sh you probably are executing it as root. That's why it works.
Give the sudo permission (without password) to the user that your php server is running.
And change the wakenas.sh to:
#!/bin/bash
sudo etherwake -D "BC:5F:F4:09:E1:07"
echo "why!?!?!" > "/var/www/html/works.txt"
exit 1
I recently published a project that allows PHP to obtain and interact with a real Bash shell (as root if requested), it solves the limitations of exec() and shell_exec(). Get it here: https://github.com/merlinthemagic/MTS
After downloading you would simply use the following code:
$shell = \MTS\Factories::getDevices()->getLocalHost()->getShell('bash', true);
$return1 = $shell->exeCmd("etherwake -D \"BC:5F:F4:09:E1:07\"");
//the return will be a string containing the return of the command
echo $return1;

Allow PHP/Apache to shell_execute commands on Ubuntu

I'm trying to execute a command through PHP with shell_exec. The PHP file is hosted by Apache on my Ubuntu server.
When I run this:
echo shell_exec("ps ax | grep nginx");
Then I get to see data. But when I run another command, for example:
echo shell_exec("cat /usr/local/nginx/config/nginx.config");
Then it's not showing anything at all. But when I copy that command and paste it in my terminal, then it executes fine.
My Apache server is running as user www-data. So I edited sudoers and added this line:
www-data ALL=(ALL:ALL) ALL
I know this is a security risk, but I wanted to make sure (for now) that www-data is able to execute all commands. But, for some reason I'm still not able to execute all commands with my PHP script.
Anyone any idea what to do?
have you read http://php.net/manual/en/function.shell-exec.php
There is quite a discussion in comments section. Top comment is:
If you're trying to run a command such as "gunzip -t" in shell_exec and getting an empty result, you might need to add 2>&1 to the end of the command, eg:
Won't always work:
echo shell_exec("gunzip -c -t $path_to_backup_file");
Should work:
echo shell_exec("gunzip -c -t $path_to_backup_file 2>&1");
In the above example, a line break at the beginning of the gunzip output seemed to prevent shell_exec printing anything else. Hope this saves someone else an hour or two.
echo shell_exec("sudo cat /usr/local/nginx/config/nginx.config");
Try that.

Command not found: PHP exec()

This is driving me crazy. I need to have php execute a command to restart a script running in node. I'm using a node app called forever to run said script. The code is as follows:
<?php
echo '<pre>';
echo exec('sudo -u dalton forever restart botti.js 2>&1');
echo '</pre>';
?>
However, when I run that, I get sudo: forever: command not found
Next, I try which forever and type forever, both which give me:
forever: /usr/local/bin/forever
I edit my code to:
echo exec('sudo -u dalton /usr/local/bin/forever restart botti.js 2>&1');
Edit: After a typo, the error is now:
/usr/bin/env: node: No such file or directory
I'm at my wit's end. Any ideas?
As the forever command only runs, when you give the full path, I suspect, that /usr/local/bin is not in your PATH environment variable, which contains all directories, that are searched for executable commands by default, separated by : (I suspect you're on Linux, may differ for other OS)
I suspect forever calls /usr/bin/env node. The error from env is probably caused by node being outside your PATH too.
To set your PATH in php, use putenv('PATH=<your path here>');
e.g. to append /usr/local/bin:
putenv('PATH=' . getenv('PATH') . ':/usr/local/bin')
This may also be a sudo issue, try the -E (preserve environment) switch.
Figured it out, I needed to define node as well:
$asdf = system('sudo -E -u dalton /usr/local/bin/node /usr/local/bin/forever restart botti.js 2>&1');
Create a symbolic link for forever
ln -s /usr/local/bin/forever /usr/bin/env/forever
And also for nodejs if incase it's still called "nodejs". Make it call as "node"
ln -s /usr/bin/nodejs /usr/bin/node
I will solve the forever execution problem.
For php side, try with this
echo shell_exec("your command sh");

Categories