lua中实现判断是否为局域网IP

2023-02-08 10:33:19    技术分享   

  脚本调用方式调用方式

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


QQ
微信