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 /tmp/shell.sock
test.sh 文件测试是否成功安装
tinywan@tinywan:~/sockproc$ ./tests.sh
=========================
status: Linux tinywan 4.8.--generic #~16.04.-Ubuntu SMP Fri Mar :: UTC x86_64 x86_64 x86_64 GNU/Linux status: uid=(tinywan) gid=(tinywan) 组=(tinywan),(adm),(cdrom),(sudo),(dip),(plugdev),(lpadmin),(sambashare) status: status: line1
line2 status: /bin/sh: : thisshouldfail: not found
status: hello output hello error
status: =========================
二、安装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.--generic #~16.04.-Ubuntu SMP Fri Mar :: UTC 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
OpenResty 扩展库之(一)——lua-resty-shell 库的更多相关文章
- LUA+resty 搭建验证码服务器
使用Lua和OpenResty搭建验证码服务器 雨客 2016-04-08 16:38:11 浏览2525 评论0 云数据库Redis版 摘要: Lua下有个Lua-GD图形库,通过简单的Lua语句就 ...
- lua luna工具库
luna工具库 概述 luna库提供了几个lua开发的常见辅助功能: lua/c++绑定 lua序列化与反序列化 变长整数编码,用于lua序列化,当然也可以方便的用于其他场合 这里把代码编译成了动态库 ...
- openresty开发系列38--通过Lua+Redis 实现动态封禁IP
openresty开发系列38--通过Lua+Redis 实现动态封禁IP 一)需求背景为了封禁某些爬虫或者恶意用户对服务器的请求,我们需要建立一个动态的 IP 黑名单.对于黑名单之内的 IP ,拒绝 ...
- 生成lua的静态库.动态库.lua.exe和luac.exe
前些日子准备学习下关于lua coroutine更为强大的功能,然而发现根据lua 5.1.4版本来运行一段代码的话也会导致 "lua: attempt to yield across me ...
- openresty开发系列24--openresty中lua的引入及使用
openresty开发系列24--openresty中lua的引入及使用 openresty 引入 lua 一)openresty中nginx引入lua方式 1)xxx_by_lua ---> ...
- 20个必不可少的Python库也是基本的第三方库
个属于我常用工具的Python库,我相信你看完之后也会觉得离不开它们.他们是: Requests.Kenneth Reitz写的最富盛名的http库.每个Python程序员都应该有它. Scrapy. ...
- Python的标准库介绍与常用的第三方库
Python的标准库介绍与常用的第三方库 Python的标准库: datetime:为日期和时间的处理提供了简单和复杂的方法. zlib:以下模块直接支持通用的数据打包和压缩格式:zlib,gzip, ...
- 系列篇|编译可在Android上运行的依赖库(一):glib库
前言 这是系列文章,它们由<编译可在Android上运行的glib库>及其他4篇文章组成,这4篇文章在“编译依赖库”一节中列出.由于glib库依赖于其他第三方库,所以需要先将依赖的第三方库 ...
- Web---JSTL(Java标准标签库)-Core核心标签库、I18N国际化、函数库
前面为JSTL中的常用EL函数,后面的为具体演示实例! JSTL简介: JSTL(Java Standard Tag Library) –Java标准标签库. SUN公司制定的一套标准标签库的规范. ...
随机推荐
- Hibernate利用纯sql
String hql = "select * from shop where shop.strid in(select strid from moneythreeshop where mon ...
- jquery前端第一讲
1.bootstrap里面的文件是什么意思: bootstrap.cssbootstrap.min.cssbootstrap-responsive.cssbootstrap-responsive.mi ...
- mysql group by分组查询错误修改
select @@global.sql_mode;set @@sql_mode ='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR ...
- 项目复审—Alpha阶段
项目复审-Alpha阶段 小组的名字和链接 优 点 缺 点 排名 [别看了你没救队]http://www.cnblogs.com/liaoyujun233/p/9016362.html 此队优点很多, ...
- 第八周PSP&进度条
团队项目PSP 一.表格: C类型 C内容 S开始时间 E结束时间 I时间间隔 T净时间(mins) 预计花费时间(mins) 讨论 讨论各个模块页面设计 9:30 12:30 站立会议 分配 ...
- Delphi 使用TAdoQuery执行存储过程的样例
procedure TCustomerForm.FindCustomerInfo;var strSql:string;begin // BL_HV_FindCustomerInfo 存储过程的名称 ...
- Delphi 判断一个字符串是否为数字
//函 数 名: IsDigit//返 回 值: boolean//日 期:2011-03-01//参 数: String//功 能: 判断一个字符串是否为数字// ...
- Java 输入/输出 反射
Java 输入/输出 反射 输入输出和反射 一.数据流的基本概念 流一般分为 ( Input Stream ) 和输出流 ( Output Stream ) 两类,但这种划分并不是绝对的.比如一 ...
- [BZOJ2244][SDOI2011]拦截导弹 CDQ分治
2244: [SDOI2011]拦截导弹 Time Limit: 30 Sec Memory Limit: 512 MB Special Judge Description 某国为了防御敌国的导弹 ...
- Qt环境配置 + Qt使用教程
官方下载链接有以下: http://download.qt.io/official_releases/qt/5.8/5.8.0/ http://download.qt.io/official_rele ...