Linux环境下使用shell编写CGI(httpd)

时间:2023-03-09 21:32:34
Linux环境下使用shell编写CGI(httpd)

/var/www/cgi-bin/hello.sh

#!/bin/bash

echo "Content-type: text/html"
echo ""

echo '<html>'
echo '<head>'
echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">'
echo '<title>Hello World</title>'
echo '</head>'
echo '<body>'
echo 'Hello World'
echo '</body>'
echo '</html>'

exit
        

赋予执行权限:

chmod +x /var/www/cgi-bin/hello.sh

如果使用SELinux,必须执行以下命令: chcon -t httpd_sys_content_t /var/www/cgi-bin/hello.sh

访问:

http://ip/cgi-bin/hello.sh

同理:

/var/www/cgi-bin/uptime.sh

#!/bin/bash

echo "Content-type: text/html"
echo ""

echo '<html>'
echo '<head>'
echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">'
echo '<link rel="SHORTCUT ICON" href="http://www.megacorp.com/favicon.ico">'
echo '<link rel="stylesheet" href="http://www.megacorp.com/style.css" type="text/css">'

PATH="/bin:/usr/bin:/usr/ucb:/usr/opt/bin"
export $PATH

echo '<title>System Uptime</title>'
echo '</head>'
echo '<body>'

echo '<h3>'
hostname
echo '</h3>'

uptime

echo '</body>'
echo '</html>'

exit 

http://192.168.1.112/cgi-bin/uptime.sh

/var/www/cgi-bin/env.sh

#!/bin/bash

echo "Content-type: text/html"
echo ""

echo '<html>'
echo '<head>'
echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">'
echo '<title>Environment Variables</title>'
echo '</head>'
echo '<body>'
echo 'Environment Variables:'
echo '<pre>'
/usr/bin/env
echo '</pre>'

echo '</body>'
echo '</html>'

exit
        

http://192.168.1.112/cgi-bin/env.sh?namex=valuex&namey=valuey&namez=valuez

参考:

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