Sometimes it can be useful with a simple CGI to show the contents of a directory, or run a command with some parameters.

If you are a programmer you probably will think at a elegant and practical solutions in Java, Ruby or PHP, but for a system administrator may be convenient to make a simple bash program, a language he’s using everyday.

Let’s see how to use the bash we use usually on the terminal in a CGI program.





All these examples must be put in a file with execution permission (by the webserver) and put on your CGI directory, in Apache this is the directory indicate with the directive ScriptAlias.

Basic Cgi

One important thing to remember is that we need to identify the MIME type of the page, that is the type of data that we transmit for example it could be text/plain, text/html or application/xml.

Otherwise we will have an error on the webserver with a message such as “Malformed header”.

#!/bin/bash echo "Content-Type: text/plain" echo echo "Today is:" date #!/bin/bash echo "Content-Type: text/plain" echo echo "Today is:" date

The MIME is identified with the 2 echo the first set the content-type and the second it’s needed to send a blank line, that second line it’s required or you’ll get an error from the webserver.

Same example but in html:

#!/bin/bash echo "Content-Type: text/html" echo echo "<HTML><BODY>" echo "<CENTER>Today is:</CENTER>" echo "<CENTER><B>" date echo "</B></CENTER>" echo "</BODY></HTML>"

Getting parameters from GET and POST methods

The easiest way to get parameters for your simple CGI is to use GET or POST method, and parse them with bashlib

bashlib is a shell script that makes CGI programming in the bash shell easier, or at least more tolerable. It contains a few functions that get called automatically and place form elements (from POSTs and GETs) and cookies in your environment. It also contains complete documentation on how to use these variables and how to set cookies manually.

With bashlib to get a specific parameter just write

... YourVariable = ` param username ` ... ... YourVariable=`param username` ...

So if your CGI is called like: http://yourserver.com/bash.cgi?name=joe&surname=smith you can use the following code to get the 2 parameters.

#!/bin/bash # this sources bashlib into your current environment . /usr/local/lib/bashlib echo "Content-type: text/plain" echo "" name=`param name` # name = joe surname=`param surname` # surname = smith echo "Hello $name $surname"

Example with bashlib

#!/bin/bash # this sources bashlib into your current environment . / usr / local / lib / bashlib echo "Content-type: text/html" echo "" # OK, so we've sent the header... now send some content echo "<html><title>Crack This Server</title><body>" # print a "hello" if the username is filled out username = ` param username ` if [ -n "x $username " ! = "x" ] ; then echo "<h1>Hello, $username </h1> fi echo " & lt;h2 & gt;Users on `/ bin / hostname `& lt; / h2 & gt; " echo " & lt;ul & gt; " # for each user in the passwd file, print their login and full name # bold them if they are the current user for user in $(cat /etc/passwd | awk -F: '{print $1 "\t" $5}') ; do echo " & lt;li & gt; " if [ " $username " = " $user " ] ; then echo " & lt;strong & gt; $user & lt; / strong & gt; " else echo " $user " fi echo " & lt; / li & gt; " done echo " & lt; / ul & gt; " echo " & lt; / body & gt; & lt; / html & gt; " #!/bin/bash # this sources bashlib into your current environment . /usr/local/lib/bashlib echo "Content-type: text/html" echo "" # OK, so we've sent the header... now send some content echo "<html><title>Crack This Server</title><body>" # print a "hello" if the username is filled out username=`param username` if [ -n "x$username" != "x" ] ; then echo "<h1>Hello, $username</h1> fi echo "<h2>Users on `/bin/hostname`</h2>" echo "<ul>" # for each user in the passwd file, print their login and full name # bold them if they are the current user for user in $(cat /etc/passwd | awk -F: '{print $1 "\t" $5}') ; do echo "<li>" if [ "$username" = "$user" ] ; then echo "<strong>$user</strong>" else echo "$user" fi echo "</li>" done echo "</ul>" echo "</body></html>"

This is a small introduction to make you start writing some simple CGI.

And if you think that you can do with bash just little things, look at this project: http://nanoblogger.sourceforge.net/

References:

Apache: CGI scritti in Bash (italian)

Bash Libraries

http://bashlib.sourceforge.net/



Bash CGI Reference Material

http://www.yolinux.com/TUTORIALS/LinuxTutorialCgiShellScript.html

http://www.ffnn.nl/pages/articles/linux/cgi-scripting-tips-for-bash-or-sh.php

http://www.acmesystems.it/?id=165

http://wiki.flexion.org/BashCGI.html

Popular Posts:

None Found