[Techtalk] Using a web browser to control stuff locally

Mary mary-linuxchix at puzzling.org
Sat Aug 20 12:29:02 EST 2005


On Sat, Aug 20, 2005, esme wrote:
> How do you invoke a shell command from a web page?

This is really a particular example of writing a CGI script. Essentially
you write a script which the webserver will run as a command. There's a
lot of documentation on the web on writing this, but a simple
implementation of, say, a command that would run "ls -l" for you would
be this:

--- Begin CGI file ---
#!/usr/bin/bash

# send HTTP headers for the web server: CGI scripts all have to do this
# basically what it does it to send the Content-type to say that we're
# sending plain text output and then two empty lines to say that the
# headers are over
echo Content-type: text/plain
echo
echo

# now we run the command in question, which in this example lists the
# files in the /tmp directory
ls -l /tmp
--- End CGI file ---

There are a few provisos:
 1. The file must be world-readable and executable so that the webserver
    can run it: chmod +rx [filename]
 2. you must place it in a web accessible directory in which you are
    allowed to put cgi scripts. This depends on your host

There are lots of CGI script tutorials on the web.

-Mary


More information about the Techtalk mailing list