Tuesday, June 3, 2014

CGI rocks.

The CGI (Common Gateway Interface) defines a way for a web server to interact with external content-generating programs, which are often referred to as CGI programs or CGI scripts. It is the simplest, and most common, way to put dynamic content on your web site. The main point of this is that you can use your own programming interface to push or pull data from a web page.

Cgi install in part. assuming apache2 is already installed.

$ sudo apt-get install libapache2-mod-perl2

$ sudo a2enmod cgi

$ sudo service apache2 restart

For example I like to use Freebasic to control ports for automation. The cgi interface allows me to do that. Actually what I wanted to talk about is changing the directory where you usually store the files. Here is a typical entry for cgi scripts location.

ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
Options +ExecCGI
AddHandler cgi-script .cgi .pl
Options FollowSymLinks
Require all granted

The current directory is just fine and probably best for admins to control.However, I would like to used a different directory. All you have to do is to change the script alias and the directory lines.

ScriptAlias /cgi-bin/ /var/wwww/cgi-bin/
<Directory "
Options +ExecCGI
AddHandler cgi-script .cgi .pl
Options FollowSymLinks
Require all granted

You will now want to restart Apache2

$ sudo service apache2 restart

Then just copy your cgi and other support files to the directory.

$ cp *.cgi /var/wwww/cgi-bin/.

Now you can use your special pages.

A bash cgi example:

    echo "Content-type: text/htmln"
    # read in our parameters
    CMD=`echo "$QUERY_STRING" | sed -n 's/^.*cmd=([^&]*).*$/1/p' | sed "s/%20/ /g"`
    FOLDER=`echo "$QUERY_STRING" | sed -n 's/^.*folder=([^&]*).*$/1/p' | sed "s/%20/ /g"| sed "s/%2F///g"`
     FOLDER1=`echo "$QUERY_STRING" | sed -n 's/^.*folder1=([^&]*).*$/1/p' | sed "s/%20/ /g"| sed "s/%2F///g"`
FOLDER2=`echo "$QUERY_STRING" | sed -n 's/^.*folder2=([^&]*).*$/1/p' | sed "s/%20/ /g"| sed "s/%2F///g"`
    # our html header
    echo "<html>"
    echo "<head><title>Bash CGI</title></head>"
    echo "<body>"
    # test if any parameters were passed
    if [ $CMD ]
      case "$CMD" in
          echo "Output of ifconfig :<pre>"
          echo "</pre>"
          echo "Output of uname -a :<pre>"
          /bin/uname -a
          echo "</pre>"
          echo "Output of dmesg :<pre>"
          echo "</pre>"
          echo "Output of df -h :<pre>"
          /bin/df -h
          echo "</pre>"
          echo "Output of free :<pre>"
          echo "</pre>"
              echo "Hardware listing :<pre>"
              echo "</pre>"
              echo "lsusb :<pre>"
              echo "</pre>"
              echo "List of users :<pre>"
              echo "</pre>"
          echo "Output of ls $FOLDER :<pre>"
          /bin/ls "$FOLDER"
          echo "</pre>"
              echo "Output of ls $FOLDER1 :<pre>"
              /bin/ls -al "$FOLDER1"
              echo "</pre>"
              echo "System to wake: $FOLDER2 :<pre>"
              /usr/bin/wakeonlan "$FOLDER2"
              echo "</pre>"
          echo "Ubuntu version :<pre>"
          /usr/bin/lsb_release -a
          echo "</pre>"
              echo "Cpu information :<pre>"
              cat /proc/cpuinfo
              echo "</pre>"
          echo "Unknown command $CMD<br>"
    # print out the form
    # page header
    echo "<p>"
    echo "<center>"
    echo "<h2>Bash commands</h2>"
    echo "</center>"
    echo "<p>"
    echo "<p>"
    echo "Choose which command you want to run"
    echo "<form method=get>"
    echo "<input type=radio name=cmd value=ifconfig checked> ifconfig (Network configuration) <br>"
    echo "<input type=radio name=cmd value=uname> uname -a (Kernel version)<br>"
    echo "<input type=radio name=cmd value=dmesg> dmesg (System messages) <br>"
    echo "<input type=radio name=cmd value=lsb_release> lsb_release (Ubuntu version) <br>"
    echo "<input type=radio name=cmd value=df> df -h (Free disk space) <br>"
    echo "<input type=radio name=cmd value=free> free (Memory info)<br>"
        echo "<input type=radio name=cmd value=cpuinfo> Cpu information <br>"
        echo "<input type=radio name=cmd value=hw> Hardware listing <br>"
    echo "<input type=radio name=cmd value=lsuser> User listing <br>"
    echo "<input type=radio name=cmd value=lsusb> lsusb (Usb ports info)<br>"
    echo "<input type=radio name=cmd value=ls> ls  -- folder <input type=text name=folder value=/mnt/flash><br>"
    echo "<input type=radio name=cmd value=lsal> ls -al -- folder <input type=text name=folder1 value=/mnt/flash><br>"
        echo "<input type=radio name=cmd value=wol> wakeonlan (enter mac address) <input type=text name=folder2 value=00:00:00:00:00:00><br>"
    echo "<input type=submit>"
    echo "</form>"
    echo "</body>"
    echo "</html>"

No comments:

Post a Comment