售后部门以前每次都需要直接登录后端服务器,执行shell脚本得到客户的分析信息。在不加功能需求的情况下,为了掐灭售后居然要登录服务器的要求,打算把这个业务简单的通过api方式对他们开放。方便你我他。
原始执行命令:bash /www/apps/tstaanagement/bin/run.sh 参数1 参数2 参数3
期望变成:浏览器输入 10.98.2.15:6666/api/run?参数1=aadf&参数2=1232&参数3=231434 得到结果
下面具体步骤:
1、安装openresty 参考:http://openresty.org/cn/installation.html
2、安装sockproc
git clone https://github.com/juce/sockproc
cd sockproc
make
./sockproc /data/tomcattemp/shell.sock
chmod 0666 /data/tomcattemp/shell.sock
2 安装lua-resty-shell模块.
git clone https://github.com/juce/lua-resty-shell
cd lua-resty-shell
cp lib/resty/shell.lua /usr/local/openresty/lualib/resty
其中/usr/local/openresty/是安装openresty的目录
3、在usr/local/openresty/lualib下创建lua脚本 写调用的命令
vim ps.lua。然后在里面写如下代码并保存
local uri = ngx.var.uri;
local args = ngx.req.get_uri_args();
local name = args["name"];
local shellCommand ="ps -ef|grep "..name
local shell = require "resty.shell"
local args = {
socket = "unix:/data/tomcattemp/shell.sock";
}
local status, out, err = shell.execute(shellCommand, args)
ngx.header.content_type = "text/plain"
ngx.say("Result:"..shellCommand.."\n" .. out)
4、增加NGINX访问入口,这样售后部门就可以通过http接口直接访问了
vi /usr/local/openresty/nginx/conf/nginx.conf
在server模块添加
location = /api/ps {
content_by_lua_file /usr/local/openresty/lualib/ps.lua;
}
保存后/usr/local/openresty/nginx/sbin/nginx -s reload 重启下NGINX
5、在postMan看看效果
10.98.2.15:80/api/ps?name=java
6、暂时没有考虑安全问题,在公司内网使用下。如果需要在外网使用则必须要考虑安全问题。