local str = (io.popen "ifconfig"):read "*a"
local st = string.find(str,"inet") + 5
local str2 = string.sub(str,st)
local en = st + string.find(str2,' ')
local ip = string.sub(str,st,en) -2
print("333________",ip)
make by 大头虾
改进
--取当前本地ip
function getLocalIp()
local str = (io.popen "ip addr"):read "*a"
local fun = function(a)
local str = string.sub(a,1,string.find(a,'/')-1)
print(str)
if str~= '127.0.0.1' then
ip = str
end
end
str=string.gsub(str,'%d*%.%d*%.%d*%.%d*/',fun)
print("__________________ip:",ip)
return ip
end
本文介绍了一个使用Lua语言编写的简单脚本,该脚本能够读取并解析系统的网络配置信息,进而提取出当前设备的本地IP地址。通过调用系统的ipaddr命令,脚本能够自动识别非环回IP地址。

3397

被折叠的 条评论
为什么被折叠?



