cgi简介以及一个用shell脚本写的shell例子分析
来源:互联网 发布:知微科技职友集 编辑:程序博客网 时间:2024/05/18 23:58
什么是cgi
cgi是common gateway interface的简称,这是一个用来处理http请求的接口;
由于http请求十分多样且十分复杂,web服务器不能完成这种操作,于是便利用了外部程序来处理这些请求,web服务器将参数传递给外部程序,外部程序将运行结果返回给web服务器;外部程序和web服务器的交互需要一种接口,那就是cgi接口,于是这些外部程序也叫cgi程序;
cgi程序不是某种语言的产物,而是一种接口,你既可以用c语言来实现cgi程序也可以用php,甚至是shell脚本来实现;
关系图如下:
外部程序是如何与web服务器进行交互的
cgi提供两种传递参数的方式:标准输入输出流,环境变量
这里的环境变量不同于操作系统提供的环境变量,这是web服务器和cgi接口所提供的用以向cgi程序传递一些重要的参数如(客户机的ip-$REMOTE-ADDR);
html中的post请求方法是用标准输入(stdin)来向cgi程序传递参数的,而html中的get请求方法则是通过环境变量QUERY-STRING来传递参数的;
下面是一些常见的cgi环境变量:
一个shell脚本写的cgi程序例子;
这个例子会用到上面写的内容,同时也有一些对shell脚本语法的解析
#!/bin/shecho "Content-type: text/plain"echo ""if [ "$REQUEST_METHOD" = "GET" ] ; then#用POST的请求方式echo GET is not expected without httpsexit fiecho "<html><head>"echo "</head><body>"echo "<pre>"ip=$REMOTE_ADDR#通过环境变量将客户机ip赋给ip变量OIFS="$IFS"IFS=\& #域分割符,作用请参考这篇博客:<a target=_blank href="http://http://blog.csdn.net/occupy8/article/details/38386985">点击打开链接</a>read user passwd inout # 从标准输入(stdin)中读取POST过来的字段;post方式传递参数的实例IFS="$OIFS"user=${user#username=} # 把开始的前缀'username='去掉(变量$user中是“username=具体名字”要将$user中的“username=”去掉,再赋给userpasswd=${passwd#password=} inout=${inout#inout=} echo "user, passwd and your ip is: $user $passwd $ip"userconf='/etc/nasuser.list' # 用grep命令判断传递进来的参数在nasuser.list文件中‘|’是ubuntu中的管道命令作用是将结果传递给下一条指令;grepstr="grep $userconf -e $user | grep $passwd" #grep是查找命令;这句话的作用是:从nasuser.list文件中找出变量$user所在的那一行,传递给grep $password;#这条命令再从结果中查找$password;userline=`$grepstr`if [ -z "$userline" ] ; then #-z判断$userline时候为空;不为空则说明传进来的用户和密码是正确的可以登录;echo "invalid user" exitfiecho okecho "" # #exitif [ ${ip:4:1} == ":" ] ; then # 构造ip6tables命令 iptcmd1="/sbin/ip6tables -D FORWARD -s $ip/128 -j ACCEPT"iptcmd2="/sbin/ip6tables -I FORWARD -s $ip/128 -j ACCEPT"iptcmd3="/sbin/ip6tables -D FORWARD -d $ip/128 -j ACCEPT"iptcmd4="/sbin/ip6tables -I FORWARD -d $ip/128 -j ACCEPT"else# 构造iptables命令iptcmd1="/sbin/iptables -t nat -D PREROUTING -s $ip/32 -j ACCEPT"iptcmd2="/sbin/iptables -t nat -I PREROUTING -s $ip/32 -j ACCEPT"iptcmd3="/sbin/iptables -t filter -D FORWARD -s $ip/32 -o enp0s3 -j ACCEPT"iptcmd4="/sbin/iptables -t filter -I FORWARD -s $ip/32 -o enp0s3 -j ACCEPT"fiecho 'before: ipv4'/sbin/iptables --list -n | grep all/sbin/iptables --list -n -t nat | grep allecho 'before: ipv6'/sbin/ip6tables --list -n | grep all/sbin/ip6tables --list -n -t nat | grep allecho ""echo "action:"echo $iptcmd1res=`$iptcmd1`echo $iptcmd3res=`$iptcmd3`if [ $inout == "I" ] ; thenecho $iptcmd2res=`$iptcmd2`echo $iptcmd4res=`$iptcmd4`fiecho ""echo 'after: ipv4'/sbin/iptables --list -n | grep all/sbin/iptables --list -n -t nat | grep allecho 'after: ipv6'/sbin/ip6tables --list -n | grep all/sbin/ip6tables --list -n -t nat | grep all#cat /etc/nasuser.listecho "</pre>"if [ $inout == "I" ] ; thenecho "<font color=red> <strong>如果出现某些可能的BUG,请关闭浏览器窗口并重新打开即可。</strong>"fiecho "</body></html>"exit
参考了以下两篇博客
cgi详解
cgi编程的get和post
0 0
- cgi简介以及一个用shell脚本写的shell例子分析
- shell写cgi脚本
- 一个解析cgi参数的SHELL脚本
- 用shell写一个简单的脚本
- 一个shell脚本例子
- Shell写的一个进程监视脚本
- 一个shell脚本写的测试框架
- 一个用shell写的执行java程序的脚本
- 用shell脚本写的一个简单的计算器
- Linux-用shell脚本写一个进度条
- 用shell写了一个自动编译代码的脚本
- 用shell脚本写的一个交互式linux命令行工具
- 用shell写了一个自动编译代码的脚本
- 一个shell脚本的分析(1)
- 一个shell脚本的分析(2)
- 一个shell脚本的分析(3)
- shell编写简单的CGI脚本
- 一个shell的例子
- SpringMVC整合Shiro
- Handler导致内存泄露分析
- Looksery Cup 2015-G. Happy Line
- 第15天 接口与抽象类的异同点以及使用场合
- html中的几种定位方式
- cgi简介以及一个用shell脚本写的shell例子分析
- LCA系列 hdu2587
- AndroidStudio中PlugNotFound解决
- linux下ssh安装与scp命令使用详解
- Java网络爬虫crawler4j学习笔记<23> IdleConnectionMonitorThread类
- BigInteger权限判断示例
- 数学分析|有关n!的一个极限(多解)
- 屏蔽双击选中中文的方法
- 重构第八章读书笔记(下)