LUA中获得服务器IP

local t = {}
-- 引入相关包local socket = require("socket")

function t.main()

        local a,b=pcall(t.execute);
        if a==false then
                logerror(a,b);
        end

end

function t.execute()
        logdebug("start");
        --local m = os.execute("ifconfig");
        --logdebug(m);
        --local s= io.popen('ifconfig')
        --local b = s:read("*all")
        --logdebug(b);
        --local t=os.execute(ip addr|grep inet|grep -v inet6|grep eth0|awk '{print $2}'|awk -F "/" '{print $1}' >> /log/app/localhost.tmp);
        --local t=os.execute("ifconfig eth0 |grep 'inet addr'| cut -f 2 -d ':'|cut -f 1 -d ' ' >> /log/app/localhost.tmp ");
        logdebug(os.time());
        local ip,resolved = socket.dns.toip(socket.dns.gethostname());
        logdebug(ip);
        logdebug(resolved);
        logdebug(t.GetAdd(socket.dns.gethostname()));
end
function t.GetAdd(hostname)
    local ip, resolved = socket.dns.toip(hostname)
    local ListTab = {}
    for k, v in ipairs(resolved.ip) do
        logdebug(k.."|"..v);
        table.insert(ListTab, v)
    end
    return ListTab
end

return t

 

posted @ 2018-08-10 14:50  浪荡小新  阅读(2876)  评论(0编辑  收藏  举报