-6

I want to remotely run some command-line program (no need for GUI) on a computer with a web server (Apache in my case) installed.

Some simple example program to start:

MyScript.sh -f file.txt

How could I do this?

As long as this could be considered a too broad question, I suggest narrowing it to (rather) simple programs, but allowing multiple ways to implement it, like HTML, JavaScript, CRON tweaks... etc.

Possible useful (but not strictly required) features:

  • View outputted results.
  • Parameters specification.

Further notes:

  • Indeed, some methods could involve a bigger security risk. It is assumed, but it is still a good idea to consider it.
John Conde
  • 217,595
  • 99
  • 455
  • 496
Sopalajo de Arrierez
  • 3,543
  • 4
  • 34
  • 52
  • Step 1: Pick a programming language (e.g. Perl). Step 2: Pick a means to link the web server to it (e.g. Plack with FastCGI). Step 3: Write a wrapper around your command line program. – Quentin May 07 '19 at 13:53
  • 1
    Flagging this as too broad... Even if **YOU** add a specific programming language Stackoverflow is not a code writing service. – dustytrash May 07 '19 at 14:05
  • @dustytrash , I can not select a programming language nor a linker to the web server (whatever that is), because I don't know which one could be used in this case. So I am posing the question. And I would say a programming site is a best site for this question than a generic or even a web design site. – Sopalajo de Arrierez May 07 '19 at 17:56
  • Possible duplicate of "Run a shell script with an html button" : https://stackoverflow.com/questions/6235785/run-a-shell-script-with-an-html-button – Sopalajo de Arrierez Jan 08 '20 at 22:41

2 Answers2

0

A very basic method :

Create some HTML file on the web server to order your program startup:

# mkdir -p /usr/local/www/apache24/data/StartScripts
# echo "Program start requested" > /usr/local/www/apache24/data/StartScripts/index.html

Create this simple script to check if somebody did access that HTML file:

$ cat CheckScriptRequest.sh
tail /var/log/httpd-access.log | grep "GET /StartScripts/"
ScriptStartupRequest=$?
if (( ScriptStartupRequest == 0 ))
then
    MyScript.sh -f file.txt
fi

Program it as a CRON entry:

$ crontab -l
* * * * * CheckScriptRequest.sh

Now, when you will navigate to http://yourdomain.com/StartScripts , you will get a message "Program start requested", and your computer will run the program MyScript.sh -f file.txt (well, it will delay one minute until the CRON tab performs the check).

Of course, this simple proof of concept has multiple flaws and could be enhanced a lot.

Sopalajo de Arrierez
  • 3,543
  • 4
  • 34
  • 52
0

An example of the CGI way (extracted from here) that shows the classic "Hello World" and today's date (thanks to Vivek Gite):

Assuming the path to CGI executables (could change between versions) is /usr/lib/cgi-bin :

$ cd /usr/lib/cgi-bin
$ cat first.cgi

#!/bin/bash
echo "Content-type: text/html"
echo ""
echo "<html><head><title>Bash as CGI"
echo "</title></head><body>"

echo "<h1>Hello world</h1>"
echo "Today is $(date)"

echo "</body></html>"

Setup exe permission on the script:

$ chmod +x first.cgi

Fire up your web browser and test the script navigating to:

http://localhost/cgi-bin/first.cgi

or remotely (open port, blah blah blah...):

http://your-ip/cgi-bin/first.cgi

Of course, you need CGI support enabled on your web server.

Sopalajo de Arrierez
  • 3,543
  • 4
  • 34
  • 52