Skip to content

How to Run Bash Script Using HTML Button in The Browser?

  • by
run bash script in browser

When we want to run a bash script, we usually run two methods. The first method is to access the server where the bash script is located or use the crontab for the second method. Is there any alternative method other than these two methods? The answer is yes. We can use one method besides using the two methods above to run a bash script that is using the HTML button in the browser.

 

Problem

How to run bash script using HTML button in the browser?

 

Solution

We can run a bash script using the HTML button in the browser using the shell_exec() or exec() or system() functions. The three functions will return the same value if we use these functions. The differences are:

  • shell_exec() returns the complete output as a string. The string can be NULL when an error occurs or if the command produces no output.
  • exec() returns the last line of the executed command as a string. NOTE: Use exec() when you require access to the program exit. shell_exec() does not provide the program exit code information.
  • system() is like shell_exec() but displays output directly (without using echo() or print()).

 

For example, we can see the OS version of our server and the memory used in the browser by typing a PHP script (for example, we name it shell.php) as below:

<?php

$output = shell_exec('cat /etc/*release | grep PRETTY_NAME;echo;free -m');
Echo '<pre>$output</pre>';

?>

 

Open a browser and it should look like this:

Running Linux command and display it in the browser

 

As seen in the image above, using the PHP command, we can run the Linux command and display it in the browser.

If we want the bash script to work after we press the HTML button in the browser, then we first create an HTML file (for example we name it index.php and I create it in the test folder) and copy the script below:

<!DOCTYPE html>
<html>
<body>

<center>
<h1>Experiment to run bash script after pressing html button in browser</h1>
<button type="button" onclick="window.location.href = 'http://<?php echo $_SERVER['HTTP_HOST']; ?>/test/test.php';">Click Me!</button>
</center>

</body>
</html>

 

Then we create a test.php file like the script below:

<?php

$output = shell_exec('./bash_script.sh');
echo "<pre>$output</pre>";

?>

 

After that, create a bash script with the name bash_script.sh as below:

#!/bin/bash

cat /etc/*release | grep PRETTY_NAME
echo
free -m
echo

 

Run the following command so that we can execute the file:

chmod +x bash_script.sh

 

Open your browser and it should look like this:

Click the Click Me! button

 

Click the Click Me! button then there should be a display like below:

After you click the Click Me! button

 

If we want the bash command to perform Linux commands such as copying files or deleting files, then we have to run the access control list. For example, we want to see at what time the user presses the Click Me! That. Then in the bash_script.sh file copy the following script:

#!/bin/bash

cat /etc/*release | grep PRETTY_NAME
echo
free -m
echo
echo "The user has been clicked the button - `date`" >> log.txt

 

Then run the following command:

setfacl -Rm u:apache:rwx /var/www/html/test

 

After that refresh the web and click the button, then in test folder should form a log.txt file.

After using ACL

 

If we want after clicking the button, the web page redirects to the page that we have specified, then we first create a file that is the destination of the redirect, for example, named finish.html. Copy below script for the finish.html file:

<!DOCTYPE html>
<html>
<body>
        <center>
        <h1>The process has been done. Please check!!</h1>
        </center>
</body>
</html>

 

And copy the below script in the test.php file:

<?php

$home_url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . '/finish.html';
$output = shell_exec('./bash_script.sh');
header('Location: ' . $home_url);
?>

 

After you click the button, PHP will process the bash script and then the website will redirect to another page.

Redirection to another page

 

Note

If we use the setfacl command on the website folder, we recommend that the public does not use the website, but the website is only for internal purposes for security reasons.

 

References

php.net
w3schools.com
anto.online
geeksforgeeks.org
stackoverflow.com
brontobytes.com

 

Print Friendly, PDF & Email
Tags:

Leave a Reply

Your email address will not be published.