通过nginx代理请求执行shell脚本 带参数

    售后部门以前每次都需要直接登录后端服务器,执行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、暂时没有考虑安全问题,在公司内网使用下。如果需要在外网使用则必须要考虑安全问题。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值