脚本调用方式调用方式
print(is_lan_ip("192.168.1.1")) -- true
print(is_lan_ip("8.8.8.8")) -- false
函数实现过程
function is_lan_ip(ip)
-- Split the IP address into its octets
local octets = {}
for octet in ip:gmatch("%d+") do
table.insert(octets, tonumber(octet))
end
-- Check if the IP is in the range of private IP addresses
if octets[1] == 10 then
return true
elseif octets[1] == 172 and octets[2] >= 16 and octets[2] <= 31 then
return true
elseif octets[1] == 192 and octets[2] == 168 then
return true
else
return false
end
end
该文章提供了一个Lua函数`is_lan_ip`,用于判断给定的IP地址是否属于局域网(LAN)地址范围。它通过检查IP的第一个和第二个八位字节来确定,匹配10.0.0.0/8,172.16.0.0/12,和192.168.0.0/16这三个私有IP地址范围。
967

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



