据说lua语言小而强大,没事翻阅了下lua的学习教程,利用其sting模和file操作模块,写了一个简单的取ip发址的demo脚本,内容如下:

#!/usr/bin/env lua
# code from www.361way.com <itybku@139.com>

h = io.popen('ifconfig')
r = h:read('*a')
h:close()
-- print(r)
for s in  string.gmatch(r,"addr:%d+%.%d+%.%d+%.%d+") do
   if s ~= "addr:127.0.0.1" then
    print(s)
   end
end

带管道的命令同样支持:

[root@ZJHZ-CMREAD-YUM01-VINT-SD ~]# lua
Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
> h = io.popen('ps auxf|wc -l')
> r = h:read('*a')  
> print(r)
140

参考页面:

Lua 教程

Lua模式匹配


donation