apache服务web页面执行shell脚本

首先安装apache服务

yum -y install httpd

如下介绍两种执行的方式

方式一、url直接传参的方式

#cat  /var/www/cgi-bin/shell
#!/bin/sh
printf "Content-Type: text/plain\n\n"
your_commands_here

传参的动作

http://172.16.61.119:8098/cgi-bin/shell?pwd

在浏览器里访问这个url就可以显示pwd路径

方式二、页面输入参数的方式

首先准备index.html的页面

#cat /var/www/html/index.html
<html>
<head>
<script>
function httpGet(url)
{
        var xmlHttp = new XMLHttpRequest();
        xmlHttp.open("GET", url, false); // false: wait respond
        xmlHttp.send(null);
        return xmlHttp.responseText;
}
function f()
{
        var url = "http://172.16.61.119:8098/cgi-bin/shell?wanyan%20"+ document.getElementById('in').value;
        document.getElementById('out').innerHTML = httpGet(url);
}
</script>
</head>
<body>
<span>专营店授权代码: </span>
<input id='in'></input>
<button onclick='f()'>提交查询</button>
<br/>
<pre id='out'></pre>
</body>
</html>

index.html页面注释:

  1. 专营店授权代码:  #这个是提示页面输入的内容,这个根据需求换成自己需要传的参数
  2. 提交查询 #这个就是sumbit的提交的动作
  3. wanyan: #这个就是引用方脚本(这个自定义),为了引用方便我放在了/usr/local/bin目录下
  4. 注意wanyan这个后边有一个空格"%20"
  5. var url = "http://172.16.61.119:8098/cgi-bin/shell?wanyan%20"+ document.getElementById('in').value; #这部分根据自己的实际情况换成自己的地址,也可以是127.0.0.1的地址,因为本身就是自己调用自己。

shell文件的内容(这里需要变更shell里的内容和直接传参的方式内容是不相同的)

#cat  /var/www/cgi-bin/shell
#!/bin/sh
alias urldecode='sed "s@+@ @g;s@%@\\\\x@g" | xargs -0 printf "%b"'
echo -e "Content-type: text/plain\n"
decoded_str=`echo $QUERY_STRING | urldecode`
echo -e "`$decoded_str` \n"

自定义脚本内容

#cat  /usr/local/bin/wanyan
#!/bin/bash
CODE=$1
curl -X POST "http://172.16.61.223:8716/bi/globe/api/queryCustomer" -H  "Request-Origion:SwaggerBootstrapUi" -H  "accept:application/json" -H  "token:" -H  "Content-Type:application/json" -d "$CODE" |jq|grep -E 'authCode|cloudShopName|shopAddress|yestaeCustomerManagerName|cloudShopAdminName|cloudShopAdminMobile|shopOwner|ownerMobile|shopLevelName'|sed --expression='s/"//g'  --expression='s/,//g' --expression='s/ //g'

wanyan这个脚本注释

  1. CODE #就是传的参数  也就是index.html页面里的专营店授权代码
  2. URL这个需要有没有的话自己写或者给开发索要
  3. 需要展示不同的内容修改wanyan这个脚本就行了(可以自定义)
  4. jq这个格式转换工具,需要yum提前安装一下

最终的页面展示的效果


版权声明:本文为ethnicitybeta原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。