通过CGI实现在Web页面上执行shell命令
实验环境:
腾讯云服务器centos7
Apache-httpd的安装:
使用命令安装 yum install httpd命令安装
yum install httpd
#安装成功我们可以看到在/var/目录下会产生一个www的目录,该目录下还包含/cgi-bin/ /html/连个目录
cgi-bin目录下主要存放cgi文件
html目录下主要存放html网页文件
此时可启动httpd服务,查看启动后的状态;
[root@VM_0_16_centos cgi-bin]# systemctl start httpd.service
[root@VM_0_16_centos cgi-bin]# systemctl status httpd.service
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
Active: active (running) since Sat 2019-10-12 22:47:27 CST; 12h ago
Docs: man:httpd(8)
man:apachectl(8)
Main PID: 9395 (httpd)
Status: "Total requests: 61; Current requests/sec: 0; Current traffic: 0 B/sec"
CGroup: /system.slice/httpd.service
├─ 9395 /usr/sbin/httpd -DFOREGROUND
├─ 9400 /usr/sbin/httpd -DFOREGROUND
├─ 9401 /usr/sbin/httpd -DFOREGROUND
├─ 9402 /usr/sbin/httpd -DFOREGROUND
├─ 9403 /usr/sbin/httpd -DFOREGROUND
├─ 9404 /usr/sbin/httpd -DFOREGROUND
├─10291 /usr/sbin/httpd -DFOREGROUND
├─10299 /usr/sbin/httpd -DFOREGROUND
└─10300 /usr/sbin/httpd -DFOREGROUND
Oct 12 22:47:27 VM_0_16_centos systemd[1]: Starting The Apache HTTP Server...
Oct 12 22:47:27 VM_0_16_centos httpd[9395]: AH00558: httpd: Could not reliab...e
Oct 12 22:47:27 VM_0_16_centos systemd[1]: Started The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.
此时打开浏览器,输入自己虚拟机IP地址:http://139.199.6.***,即可看到apache的默认界面;
服务器搭建完成创建shell脚本
服务器通常会有一个www/cgi-bin的目录,我在这个目录下放一个shell脚本,名为liu.sh,记得给执行权限。
[root@VM_0_17_centos cgi-bin]# vim liu.sh
[root@VM_0_17_centos cgi-bin]# chmod 777 liu.sh
[root@VM_0_17_centos cgi-bin]# cat -n liu.sh
1 #!/bin/sh
2 alias urldecode='sed "s@+@ @g;s@%@\\\\x@g" | xargs -0 printf "%b"'
3 echo -e "Content-type: text/html\n"
4 decoded_str=`echo $QUERY_STRING | urldecode`
5 echo `$decoded_str`
[root@VM_0_17_centos cgi-bin]#
第1句表示是shell脚本,shell是默认的脚本
第2句我网上抄的,具体原理也不懂,作用是解码URL, 当URL中有空格时,从客户端传过来会变成%20, 20是空格的16进制ASCII码。
第3句是必须的,否则在客户端调用时就出错,是http协议规定的。text/html是以html的形式输出,比如就会在页面上显示一个文本框。text/plain形式就会在页面上原样显示这段代码。
第4句就是将URL解码
第5句是执行命令并返回给客户端
然后在web网页输入http://139.199.6.165/cgi-bin/liu.sh?ls 会在网页上显示内容
就不是很好看,然后我先写一个html文件,放在www/html文件夹下,命名为hjw.html
[root@VM_0_17_centos html]# vim hjw.html
[root@VM_0_17_centos html]# cat -n hjw.html
1 <html>
2 <head>
3 <script>
4 function httpGet(url)
5 {
6 var xmlHttp = new XMLHttpRequest();
7 xmlHttp.open("GET", url, false);
8 xmlHttp.send(null);
9 return xmlHttp.responseText;
10 }
11 function f()
12 {
13 var url = "http://139.199.6.165/cgi-bin/liu.sh?"
14 + document.getElementById('in').value;
15 document.getElementById('out').innerHTML = httpGet(url);
16 }
17 </script>
18 </head>
19 <body>
20 <span>command </span><input id='in'></input>
21 <button οnclick='f()'>send</button>
22 <br/>
23 <pre id='out'></pre>
24 </body>
25 </html>
[root@VM_0_17_centos html]#
在web浏览器中输入http://139.199.6.165/hjw.html,在方框输入命令下面就直接显示结果,也就是上面的一个包装。
到此就应该可以实现将服务器执行结果显示到网页上。