简体   繁体   中英

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:

<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?

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM