Lua 获取本地IP
Lua 获取本地IP
1、代码:
--[[ author:lizhm datetime:20220706 09:38:00 description:获取本地IP ]] -- 引入socket模块 local socket = require("socket") -- 打印时间 print(os.time()) -- 功能函数 function getIp(hostname) local ip, resolved = socket.dns.toip(hostname) -- 初始化表listTab local listTab = {} for k, v in ipairs(resolved.ip) do -- listTab表中插入值v table.insert(listTab, v) end return listTab end print(unpack(getIp('localhost'))) print(unpack(getIp(socket.dns.gethostname())))
结果: