美文网首页
OpenResty扩展库之lua-resty-shell详解

OpenResty扩展库之lua-resty-shell详解

作者: 专修虚拟机 | 来源:发表于2019-07-31 16:26 被阅读0次

    lua-resty-shell扩展库

    一、介绍:

    • 用于非阻塞的执行shell命令的Lua模块
    • 简单说就是我们可以通过OpenResty实现远程执行shell命令,并且执行调用过程是非阻塞的。

    二、用法:

    lua-resty-shell 扩展模块有两个不同的版本(两个版本的使用方式不同)

    1. openresty官方内置的lua-resty-shell扩展模块

    2. 麻省理工Juce版本

    这两种版本,第一种内置版本需要OpenResty版本5.0以后才支持,对于老版本的openresty需要做升级处理。第二种Juce版,支持所有版本的openresty,但需要做一定的配置。两种版本的性能差异暂时没有测试,选择哪种版本需根据自身的实际情况来做选择。

    openresty官方内置扩展模块详解
    • 介绍
    • 调用示例
      第1步:安装配置OpenResty.
      第2步:编写调用shell命令的lua脚本.
      第3步:修改OpenResty的nginx配置文件.
      第4步:测试.
    • 代码解读
    • 原理总结
    麻省理工Juce版本详解
    • 介绍
      当您需要执行子进程(或shell命令)时,这是一个打算与OpenResty应用程序一起使用的小型库。 它类似于os.execute和io.popen,除了它是完全非阻塞的,因此即使对于需要很长时间完成的命令也是安全的。

      该库依赖于您需要在Web服务器(sockproc)上运行的守护程序组件。 基本思想是,shell库连接到sockproc守护程序的unix域套接字,发送命令以及子程序所期望的任何输入数据,然后读取退出代码,输出流数据和错误流数据 子进程。 因为我们使用由lua-nginx-module提供的co-socket API,所以nginx工作者从不被阻止。

    • 调用示例
      第1步:安装配置OpenResty.
      OpenResty 安装请参考:https://www.jianshu.com/p/a55caccdf252

      第2步:安装 sockproc.
      sockproc下载地址:https://github.com/juce/sockproc

      我将sockproc安装~/home/tools/文件夹下,执行以下6条命令进行安装:
      cd /home/tools
      
      git clone https://github.com/juce/sockproc.git
      
      cd sockproc/
      
      make
      
      ./sockproc /tmp/shell.sock
      
      chmod 0666 /tmp/shell.sock 
      
      安装完毕,执行以下两条命令测试是否安装成功:
      cd /home/tools/sockproc
      sh tests.sh 
      命令返回结果:
      =========================
      status:0
      114
      Linux tinywan 4.8.0-46-generic #49~16.04.1-Ubuntu SMP Fri Mar 31 14:51:03 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
      0
      status:0
      129
      uid=1000(tinywan) gid=1000(tinywan) 组=1000(tinywan),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),113(lpadmin),128(sambashare)
      0
      status:0
      2
      2
      0
      status:0
      14
      line1
      line2
      0
      status:32512
      0
      38
      /bin/sh: 1: thisshouldfail: not found
      status:0
      13
      hello output
      12
      hello error
      status:0
      

      sockproc有两种启动方式:

      • With UNIX domain socket:$ ./sockproc /tmp/shell.sock
      • With TCP socket:./sockproc 13000 (13000为端口号)

      第3步:编写调用shell命令的lua脚本.
      编写lua脚本,脚本名称自己定义,我的lua脚本名称是test-shell.lua。脚本存放位置自己定义,我的脚本存放在/home/mycode/lua/文件夹下。
      test-shell.lua脚本内容如下:

         local shell = require("resty.shell")
      
         local args = {
             socket = "unix:/tmp/shell.sock",
         }
      
         local status, out, err = shell.execute("ls", args)
      
         ngx.header.content_type = "text/plain"
         ngx.say("Shell ls Result:\n" .. out)
      
      

      注意:

      • 当sockproc的启动方式是【With UNIX domain socket】时, local args = { socket = "unix:/tmp/shell.sock"}
      • 当sockproc的启动方式是【With TCP socket】时,local args = {socket = {host="127.0.0.1", port=13000}}

      第4步:修改OpenResty的nginx配置文件.

      • 1:找到配置文档nginx.conf。
      • 2:在http模块下找到server模块,然后再server模块中新增配置
      location  /api/ls {
                content_by_lua_file /home/mycode/lua/test-shell.lua;
      }
      
      

      content_by_lua_file配置项后面的路径为调用shell命令的lua脚本

      
       worker_processes  1;
       events {
           worker_connections  1024;
       }
       http {
           include       mime.types;
           default_type  application/octet-stream;
           sendfile        on;
           
           server {
               listen       80;
               server_name  localhost;
               lua_code_cache off;    //关闭代码缓存,方便调试。
      
               location / {
                   root  /home/hexo_yzc/public;
                   index  index.html index.htm;
               }
               location  /api/shell {
                           content_by_lua_file /home/mycode/lua/test-shell.lua;
                }
               
               error_page   500 502 503 504  /50x.html;
               location = /50x.html {
                   root   html;
               }
           }
       }
      
      

      配置中的lua_code_cache off 可以关闭lua代码缓存,方便调试

      第5步:测试.
      浏览器输入:127.0.0.1(自己云主机ip)/api/shell 进行测试。

    • 代码解读

    • 原理总结

    相关文章

      网友评论

          本文标题:OpenResty扩展库之lua-resty-shell详解

          本文链接:https://www.haomeiwen.com/subject/vuoarctx.html