html5执行shell,利用nginx执行Shell 脚本

通过远程url调用

首先自行安装openresty,不做介绍了就.

假如我们安装路径为:/southtv/openresty

1 首先安装sockprocgit clone https://github.com/juce/sockproc

cd sockproc

make

./sockproc /tmp/shell.sock

chmod 0666 /tmp/shell.sock

sockproc 是一个服务器程序, 侦测unix socket 或者 tcp socket , 并把收到的命令,传递给子进程执行,执行完毕后,把结果返回给客户端, 我们就让sockproc 侦测/tmp/shell.sock 的套接口有没有数据到来.

2 安装lua-resty-shell模块.

它是一个很小的库, 配合openresty 使用, 目的是提供类似于os.execute 或io.popen的功能, 唯一区别它是非阻塞的, 也就是说即使需要耗时很久的命令,你也可以使用它git clone https://github.com/juce/lua-resty-shell

cd lua-resty-shell

cp lib/resty/shell.lua /southtv/openresty/lualib/resty/ 这是你的项目路径

3 创建自己的命令调用lua 脚本vim /southtv/openresty/lualib/command.lua --名字我起名为command.lua

local shell = require "resty.shell"

local args = {

socket = "unix:/tmp/shell.sock", --这是第一步的unxi socket

}

local status, out, err = shell.execute("ls", args) --ls 是想调用的命令,

ngx.header.content_type = "text/plain"

ngx.say("Result:\n" .. out) -- 命令输出结果

4 更改nginx 配置vim /southtv/openresty/nginx/conf/nginx.conf

#增加一个localtion 配置

location = /api/ls {

content_by_lua_file /southtv/openresty/lualib/command.lua;

}

重启 nginx

/southtv/openresty/nginx/sbin/nginx -s reload

1

5 测试效果了

可以用浏览器直接打开:

版权属于:逍遥子大表哥

按照知识共享署名-非商业性使用 4.0 国际协议进行许可,转载引用文章应遵循相同协议。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值