使用shell寫cgi,並獲取html傳遞的參數


from http://linuxaria.com/article/bash-cgi?


tux-apacheSometimes 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

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 withbashlib

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`
...

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 "&lt;html&gt;&lt;title&gt;Crack This Server&lt;/title&gt;&lt;body&gt;"
 
# print a "hello" if the username is filled out
username=`param username`
if [ -n "x$username" != "x" ] ; then
echo "&lt;h1&gt;Hello, $username&lt;/h1&gt;
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;"

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:

Find me on Google+

flattr this!


方法二:使用環境變量

from http://www.linuxidc.com/Linux/2011-03/33159.htm

查看環境變量的方法:寫一個shell.cgi,內容如下:

#! /bin/bash

. /usr/local/bin/bashlib
echo "Content-type: text/plain"

echo ""

#/usr/bin/env

將shell.cgi放入cgi-bin中,訪問http://localhost/cgi-bin/shell.cgi

就可以看到輸出的環境變量。



注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2020 ITdaan.com