CGI按照百度百科的定义,如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<IfModule alias_module> ScriptAlias /cgi-bin/ "/usr/local/apache/cgi-bin/" < /IfModule > <IfModule cgid_module> #Scriptsock cgisock < /IfModule > <Directory "/usr/local/apache/cgi-bin" > AllowOverride All Options None Require all granted < /Directory > |
进入Apache下面的cgi-bin目录,创建一个文件,文件名为index.cgi,文件扩展名为.cgi,这个和php文件的扩展名是.php是一样的。
index.cgi的内容如下:
1
2
3
4
5
6
7
|
#!/bin/bash #index.cgi echo "Content-Type:text/html;charset=utf-8" echo echo "hello world" |
然后在浏览器中访问localhost/cgi-bin/index.cgi,你就会看到结果如下:
然后将index.cgi的内容改一下:
1
2
3
4
5
6
7
8
9
|
#!/bin/bash #index.cgi echo "Content-Type:text/html;charset=utf-8" echo mysql= "mysql -uroot -proot" sql= "show databases" $mysql -e "$sql" |
浏览器运行如下:
其实,从上面的例子中,你就会得出一点结论,cgi就和php类似,只不过php文件中使用的使用php的语法,cgi中使用的shell命令,但是,都可以通过浏览器来运行脚本,获得结果。
现在看一下CGI的定义:CGI 应用程序能与浏览器进行交互,还可通过数据库API 与数据库服务器等外部数据源进行通信,从数据库服务器中获取数据。格式化为HTML文档后,发送给浏览器,也可以将从浏览器获得的数据放到数据库中。
是不是有点理解了。
################
cgi+perl+ajax初探
本来任务要求的是用perl实现cgi脚本即可。但是普通的方法都会刷新页面。我想要的结果是不刷新页面,在原页面上进行交互。即通过cgi返回的信息可以作为原网页的部分内容。所以这需要用到ajax。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
<script type= "text/javascript" > function loadXMLDoc() { var xmlhttp; if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp= new XMLHttpRequest(); } else { // code for IE6, IE5 xmlhttp= new ActiveXObject( "Microsoft.XMLHTTP" ); } var url= "" ; var name=document.getElementsByName( "name" )[0].value; var age=document.getElementsByName( "age" )[0].value; var gender=document.getElementsByName( "gender" )[0].value; var email=document.getElementsByName( "email" )[0].value; url= "?name=" +name+ "&age=" +age+ "&gender=" +gender+ "&email=" +email; alert(url); xmlhttp.open( "GET" , "http://www.cnblogs.com/cgi-bin/infoQuesAdd1.pl" +url, false ); xmlhttp.send( null ); //document.getElementById("displayboard").innerHTML=xmlhttp.responseText; alert(xmlhttp.responseText); } </script> |
1.in oracle user :we running the web program in bash to collect file
sh cash.sh db dbfcs11 58.2.101.168 1528 fcs
2.in web
部署方法:$ cd /var/www/html/doc
-》html 目录部署在 /var/www/html/doc
$ # copy the tar.gz file from github here
$ gzip -d khailey-wash-155931c.tar.gz # name generated for github download
$ tar xvf khailey-wash-155931c.tar
$ mv khailey-wash-155931c/* .
change1:
edit cgi-bin/json_ash.sh
add:
my $MON_HOME="/tmp/MONITOR";
change2:
# 因为AJAX 需要跑的CGI 脚本 ,CGI 在 httpd.conf 配置信息如下:
ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
-》CGI 部署位置/var/www/cgi-bin/
所以需要迁移文件
move all file in /var/www/html/doc/cgi-bin to /var/www/cgi-bin/
then use test command 确定能否返回数据:
http://10.200.210.187/cgi-bin/json_ash.sh (OK)
http://10.200.210.187/cgi-bin/json_ash.sh?points=60&vdb=58.2.101.168:fcs&type=0 (OK)
总结:如果
如果查不到数据:IE 界面 最下面 如下
debug data:
http://10.200.210.187/doc/html/ash.html?q=58.2.101.168:fcs 58.2.101.168:fcs z errorstart errorend /cgi-bin/json_ash.sh?points=60&vdb=58.2.101.168:fcs&type=0 complete
如果查得到数据,IE 界面 最下面 如下:
/cgi-bin/json_ash.sh?points=60&vdb=58.2.101.168:fcs&type=db,M000 102 complete