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).
Related
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 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.
I have a question that have been asked many time over the web, but none of the solutions help my case.
I need a very simple website with a single button, by which on click, a single shell command is executed to deploy a docker container.
For this I have 3 files all located in /var/www/html/:
1- Depl-Script (a simple Linux file, made executable by chmod +x and chmod 777, and does not have .sh extension, including a single command:
docker run -it -p 8080:8080 surrogate
2- a DepGUI.php
<?php
// $output=shell-exec("/var/www/html/Depl-Script"); //didn't work
$output=shell_exec("./Depl-Script"); //this doesn't work neither
echo $output; //prints 0
$output1=shell_exec("whoami"); //prints www-data
echo $output1;
?>
3- a Main.html, including:
<form action="DepGUI.php">
<input type="submit" value="Open Script">
</form>
Shell-exec doesn't work for running the shell script.
How do I know it doesn't work? I check docker containers, nothing is created.
What solutions I have tried:
1) The shell script (Dep-Script) works fine when I run it from CLI, by root user.
2)Just to make sure, I changed the etc/passwd file, found the line with www-data user and changed it's login shell to /bin/bash, and then by www-data (apache) user, I executed the same shell script, and it works. (I returned the /etc/passwd file to the original shape)
3)I have checked the /etc/php5/apache2/php.ini and /etc/php5/cli/php.ini and the exec, and shell_exec is not disabled.
4) I have checked the shell-exec and exec command with "sh" parameter, it did not work.
Any help/comment is appreciated in advance.
Thanks
Update:
I redirected errors to the output,
$output=shell_exec("./Depl-Script 2>&1");
I noticed the error is related to docker, "The input device is not a TTY" , then I got rid of -t in the docker command in the script (Dep-Script) and the error disappeared. I got a new error related to docker permissions and solved it using following link:
https://techoverflow.net/2017/03/01/solving-docker-permission-denied-while-trying-to-connect-to-the-docker-daemon-socket/
I have a bash script that takes a parameter is called in PHP by shell_exec(script.sh parameter). Basically, my goal is to call a script that is owned by another user that is not apache.
The script.sh script is a file that contains the following (right now there are some error handling commands):
#/bin/bash
whoami>>whoami
echo $1 >> parameter
while read f; do
env>>envoutput
sudo -i -u archivescriptowner /path/to/archivescript.sh -command archive >> output
done < $1
In my /etc/sudoers file , I have the following:
apache ALL=(archivescriptowner) NOPASSWD: /bin/bash -c /path/to/archivescript.sh *
When I run this script as by running su -s /bin/bash apache and pass a parameter, it works.
When I run it via my button in php, archivescript.sh does not execute
The whoami file has apache written to it
The parameter file has the right file written to it
env shows the following
Term=xterm
LD_LIBRARY_PATH=/path/to/library
PATH=/sbin/:usr/sbin:/bin:/usr/bin
PWD=/var/www/html
LANG=C
SHLVL=4
=/bin/env
PWD is outputting right, that is where my script is right now, it will be moved in the future.
The output file when it is ran by the button click is blank.
I am at a loss as to why this is not working. Any insight would be helpful. Please let me know if I need to give any additional information.
I recently published a project that allows PHP to obtain and interact with a real Bash shell. 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('/path/to/archivescript.sh');
echo $return1; //return from your script
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;