OpenResty 擴展庫之(一)——lua-resty-shell 庫


介紹

  當您需要執行子進程(或shell命令)時,這是一個打算與OpenResty應用程序一起使用的小型庫。 它類似於os.execute和io.popen,除了它是完全非阻塞的,因此即使對於需要很長時間完成的命令也是安全的。

  該庫依賴於您需要在Web服務器(sockproc)上運行的守護程序組件。 基本思想是,shell庫連接到sockproc守護程序的unix域套接字,發送命令以及子程序所期望的任何輸入數據,然后讀取退出代碼,輸出流數據和錯誤流數據 子進程。 因為我們使用由lua-nginx-module提供的co-socket API,所以nginx工作者從不被阻止。

一、安裝 sockproc

下載地址:https://github.com/juce/sockproc

具體安裝步驟:

git clone https://github.com/juce/sockproc.git

cd sockproc/

-- 通過gcc 編譯生成一個可執行的文件 sockproc gcc
-o sockproc ./sockproc.c tinywan@tinywan:~/sockproc$ ls LICENSE Makefile README.md sockproc sockproc.c tests.sh ./sockproc /tmp/shell.sock chmod 0666 /tmp/shell.sock

test.sh 文件測試是否成功安裝

tinywan@tinywan:~/sockproc$ ./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
2041
123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
0
=========================

二、安裝lua-resty-shell

git clone https://github.com/juce/lua-resty-shell

復制 shell.lua 文件到自己的項目庫中去

/home/tinywan/Openresty_Protect/First_Protect/lualib/resty

sudo cp /home/tinywan/lua-resty-shell/lib/resty/shell.lua ./

三、測試Lua執行shell 命令

nginx.conf 配置

# shell
location /shell_test {
    content_by_lua_block {
        local shell = require("resty.shell")

        local args = {
            socket = "unix:/tmp/shell.sock",
        }

        local status, out, err = shell.execute("uname -a", args)

        ngx.header.content_type = "text/plain"
        ngx.say("Hello from:\n" .. out)
    }
}

重啟nginx

sudo /opt/openresty/nginx/sbin/nginx -s reload

curl 測試結果

tinywan@tinywan:~/Openresty_Protect/First_Protect$ curl http://127.0.0.1/shell_test
Hello from:
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

可能遇到的錯誤

2017/05/04 20:50:04 [crit] 94338#0: *372128 connect() to unix:/tmp/shell.sock failed (13: Permission denied), client: 127.0.0.1, server: localhost, request: "GET /shell_test HTTP/1.1", host: "127.0.0.1"

解決辦法:chmod 0666 /tmp/shell.sock

 

小知識積累(C語言的第一個C程序案例):

收集時間:2017-05-04 21:38:23 

 

1、系統頭文件使用 一堆 <> 括起來

2、自己定義的頭文件使用 一對 “” 雙引號括起來

3、stdio.h 標准的輸入(scanf)輸出(屏幕終端)頭文件

4、入口文件 main ,也就是主函數

5、void 表示不接受任何參數

6、return 0 返回一個整形int 

7、\n 表示換行符

8、return 0 ,0 之外的數值

9、保存、編譯

tinywan@tinywan:~/C$ vim hello_world.c
tinywan@tinywan:~/C$ ls
hello_world.c
tinywan@tinywan:~/C$ gcc -o hello_world hello_world.c 
tinywan@tinywan:~/C$ ls
hello_world  hello_world.c
tinywan@tinywan:~/C$ ./hello_world  -- 通過prinf 函數輸出的文件
Hello World

10、-o 指定一個輸出文件,這里為hello_world   hello_world.c 為源文件

11、不加輸出文件編譯

 

telnet命令執行:telnet 127.0.0.1 5000

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM