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

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

就可以看到输出的环境变量。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值