I am working with 3 scripts with to goal to command a shell script with an HTML button.
My shell script launch simply 'nautilus', a linux file browser:
#!/bin/bash
nautilus
exit 0
The PHP script call the shell script like this:
<?php
exec('./test.sh');
?>
And at least, my HTML page is like this:
<!DOCTYPE html>
<html>
<head>
<style>
</style>
</head>
<body>
<input type="button" onclick="openApp.sh" value="Open Script">
</body>
</html>
The 3 scripts are located in
/var/www/html
with the rights :
sudo chown www-data:www-data /var/www/
What I am doing wrong because it doesn't work !
But when I launch this command:
cd /var/www/html
php script.php
it call correctly the shell script which launch nautilus.
Do you have an idea of what is wrong ?
Thanks
When you run the command from PHP it is executing in a text-only shell so it will not create a visible GUI.
If running through apache/nginx, PHP normally runs as a user called www-data.
You need to determine the username of the user running the graphical display, the display number and the authorization cookie.
See this conversation for more details
https://unix.stackexchange.com/questions/1596/can-i-launch-a-graphical-program-on-another-users-desktop-as-root
After you have determined the Display. Your test.sh script will look something like this
DISPLAY=:0
XAUTHORIZATION=~/.Xauthority
su username -c "nautilus" # replace username with username of your desktop/graphical user
Also be careful not to let outside users run commands on the server with PHP.
Related
I'am running Satisfactory gameserver trough lgsm deployement, which gives me access to easy commands managing the server. In this case I want to give my friends the possibility to also run "./sfserver restart" on the shell to restart the server due to the current patch being unstable and causing client crashes which when if reconnecting to the in like under 15min has the possibility to make you lose your inventory.
So I have this simple html page with a submit button that sends input 'restart' to a php script that is supposed to and in current state seems to run the "./sfserver restart" on the shell, but even though it prints out the shell output that looks correct the server is not restarted.
I have given the rights to www-data user to run commands as satisfactory in sudoers file.
www-data ALL=(satisfactory:satisfactory) NOPASSWD:ALL
Html page code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Server restart</title>
</head>
<body>
<form action="RestartScript.php" method="post" enctype="multipart/form-data">
Restart server:
<input type="submit" name="restart" value="restart">
</form>
</body>
</html>
Php script:
<?php
if (isset($_POST['restart'])) {
echo "Restarting...Please wait for 1 minute...";
$output = exec('sudo -u satisfactory /bin/bash -c "cd /home/satisfactory/ && ./sfserver restart"');
echo "<pre>$output</pre>";
echo "Restarted successfully";
}
?>
So when I click the restart button on the html page it runs the php script and the output I get in browser is and the server is not restarted:
Restarting...Please wait for 1 minute...
[K[ .... ] Starting sfserver: sfserver
[K[[32m OK [0m] Starting sfserver: sfserver
Restarted successfully
This is the output I get in the shell when running the restart command and the server gets restarted:
^[[K[ .... ] Stopping sfserver: sfserver
^[[K[ .... ] Stopping sfserver: Graceful: CTRL+c
^[[K[ .... ] Stopping sfserver: Graceful: CTRL+c: 1
...
^[[K[ .... ] Stopping sfserver: Graceful: CTRL+c: 30
^[[K[^[[31m ERROR ^[[0m] Stopping sfserver: Graceful: CTRL+c: ^[[31mFAIL^[[0m
^[[K[ .... ] Stopping sfserver: sfserver
^[[K[^[[32m OK ^[[0m] Stopping sfserver: sfserver
^[[K[ .... ] Starting sfserver: sfserver
^[[K[^[[32m OK ^[[0m] Starting sfserver: sfserver
So the script definitely runs the command I'm wanting it to, but doesn't do it propely. So I'm guessing it's some kind of permission problem still even though the satisfactory user is part of www-data group and the /home/satisfactory/ is satisfactory:www-data and file permissions 2770 on top of the sudo -u allowance.
I have tried running the script without the "sudo -u" which then makes www-data as the user running the "./sfserver restart" and that causes the sfserver shell script to spew out a list files that www-data needs to have ownership of for the sfserver to work.
I have also tried calling a shell script from the php script with the "sudo -u satisfactory" and the result is the same as with directly calling the "./sfserver restart"
Restart shell script:
#!/bin/bash
/home/satisfactory/sfserver restart
Also I've messed around with the file permissions and even with full 2777 nothing changes.
Also I've gone trough the sfserver shell script if I could find some obvious solution trough there but to no avail. It's a really elaborate shell script that calls out function shell scripts and trying to run the restart function script directly doesn't result in anything.
Help with getting my script working or alternate solution is greatly appreciated!
I am super new to web server developing and I have this server in which I want to run a bash script on a PHP script. Basically, I have this HTML file and when I click this button, it opens a online terminal, courtesy of ttyd. To start the terminal, I have to run:
ttyd -p 8080 bash
So far this is in my HTML page:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<br>
<form action="/terminal/terminal.php">
<input type="submit" value="Server Command Line Terminal">
</form>
<br>
</body>
</html>
My PHP script is shown here:
<?php
shell_exec("/var/www/html/terminal/ttyd-terminal.sh");
sleep(1);
header('Location: http://10.0.0.199:8080');
?>
And this is my bash script:
#!/bin/bash
eval 'ttyd -p 8080 bash'
So far, when I go to the site, the terminal will not open, I have tried many solutions and none them have worked, I just get a timed out response from the server, I have also tried echo 'ttyd -p 8080 bash' but that does not work either.
The problem lies in the bash script. For anybody who wants to make a .sh script executable, do the following:
chmod +x file.sh
This solved my problem and now everything works fine.
I am trying to use an html form action pointed to a PHP file in order to execute a .sh script in the background in Ubuntu server.
Click here (moodle) > php executes bash and redirects the web page > script runs and starts a headless vm.
php file is saved to /var/www/html/moodle
script is saved to /etc/init.d
html code is obviously embedded in the site
<form action="http://xxx.xxx.xxx.xxx/moodle/scriptname.php">
<input type="submit" value="Button name">
</form>
php configuration
<?php
putenv("PATH=/etc/init.d/:" .$_ENV["PATH"]."");
$output = "<pre>".shell_exec("scriptname.sh")."</pre>";
echo $output;
header('Location:http://XXX.XXX.X.XX/moodle/mod/page/view.phpid=133&forceview=1');
?>
Script
#! /bin/sh
# /etc/init.d/StartVM
#
#Edit these variables!
VMUSER=myusername
VMNAME="nameofvm"
echo "Starting VirtualBox VM..."
sudo -H -b -u $VMUSER /usr/bin/VBoxVRDP -s "$VMNAME"
exit 1
;;
esac
exit 0
I cant figure out why the this wont work. The script produces results when ran in terminal. The html code redirect the web page according to the php file, but the script will not execute when clicking the button in moodle.
Any help is appreciated.
Thanks
MAC
In your script, use the full path for sudo (/usr/bin/sudo) and modify your sudoers file if needed (Pointed out in the comments by Lawrence Cherone).
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!
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;