Executing Linux commands from webpage
Executing Linux commands from webpage
I have set up a very basic website on apache2 server (running on Ubuntu 18.04). The website has just one page. I used some JavaScript to add dynamic content to it, and the website contains HTML form, which sends data to MySql server -- I connected to the database with PHP.
Based on user input, I need to execute a certain Linux terminal command (say: ls -l
). For example, if user inputs first and last names and clicks on Submit button, the ls -l
command will automatically execute without user doing anything in terminal. Is this possible?
This is the HTML form I have:
ls -l
ls -l
<form method="post">
<label>Fisrt Name: <input type="text" name="firstname" required></label>
<label>Last Name: <input type="text" name="lastname" required></label>
<input type="submit" name="submit" value="Submit">
</form>
Edited:
Ok so I have this PHP code right now in addition to code that connects to MySql database:
<?php
$output = shell_exec('sudo addgroup test2');
echo "<pre>$output</pre>;
$result = exec('echo $?');
echo "$result";
?>
However, it does not add this group and echo $?
returns 0. However, when I substitute shell_exec('sudo addgroup test2');
with shell_exec('ls -l');
-- it works fine. What am I doing wrong?
echo $?
shell_exec('sudo addgroup test2');
shell_exec('ls -l');
ls -l
So I do not really know yet how to do that in server side code. I need some guidance
– tera_789
Jun 30 at 5:35
php.net/manual/en/function.exec.php
– Jaromanda X
Jun 30 at 5:35
Thanks. So this code will be run on the server the website is hosted, correct?
– tera_789
Jun 30 at 5:37
which code? you haven't shown any code
– Jaromanda X
Jun 30 at 5:38
1 Answer
1
This need to be handled by the backend server code running behind Apache. You may pass the commands as a query string or as POST json from UI and write a server-side script (in php) to catch the parameters and execute the function as shell-exec
http://php.net/manual/en/function.shell-exec.php .
shell-exec
Please check the Edited part
– tera_789
Jun 30 at 6:07
Try this snippet.
<?php $output = shell_exec('touch /tmp/created_from_php'); echo("<pre>$output</pre>"); ?>
To verify check /tmp/created_from_php
path after executing.– Prabuddha Chakraborty
Jun 30 at 6:13
<?php $output = shell_exec('touch /tmp/created_from_php'); echo("<pre>$output</pre>"); ?>
/tmp/created_from_php
A reason for
sudo addgroup test2
is the user php is running may not have sudo/root
priveledge. Like if you are running php with www-data
user then it may not have sudo
access.– Prabuddha Chakraborty
Jun 30 at 6:15
sudo addgroup test2
sudo/root
www-data
sudo
Replace above shell_exec function with simple sudo commands like
sudo touch /etc/created_from_php
and if it does not work then it is the php user privilege issue.– Prabuddha Chakraborty
Jun 30 at 6:18
sudo touch /etc/created_from_php
I have tried
shell_exec('touch /tmp/created_from_php');
but it did not work out for me. And you are right, it looks like I am running php with www-data
user. When I do exec('whoami');
-- it returns www-data
– tera_789
Jun 30 at 6:31
shell_exec('touch /tmp/created_from_php');
www-data
exec('whoami');
www-data
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
so,
ls -l
will be run on the server - so you'd need to show how you intend to do that in server side code– Jaromanda X
Jun 30 at 5:33