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.
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 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.
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 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!