Bash – What’s needed to have a shell script run via HTTP Post request

bashhttpshell-scriptwebserver

I was wondering what is needed to trigger a shell script with a HTTP post request.
Being in my home wifi I want to call http://ip.of.host/req?run and have a script triggered on my host.
I guess I have to run a webserver on the host. But what else is needed in addition?
Is this the right place to ask that kind of question?
Thanks!

Best Answer

This is a very broad question. I would install Apache and PHP on the server and then you can use a very simple PHP script to run the script. Nothing else should be required.

In your web root directory, place a script and a php file:

script.sh:

#!/bin/bash

echo "Hello World"

run_script.php:

<?php
    $output = shell_exec('./script.sh');
    echo $output;
?>

Run this using localhost/run_script.php.

You can then run any script you want (you should not place it in your web root!)

If you only want to run this on an HTTP Post request, then wrap the script in

if ($_SERVER['REQUEST_METHOD'] === 'POST') { ... }

And you can check for Post parameters with

if ($_POST['parameter']) { ... }

Note: There are many caveats with this, but they are better addressed as separate questions. This should get you going, anyway.

Related Question