-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathexample.lua
More file actions
34 lines (28 loc) · 722 Bytes
/
Copy pathexample.lua
File metadata and controls
34 lines (28 loc) · 722 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
local ipaddr = require('ipaddr')
local insert = table.insert
local sort = table.sort
local function compare_networks(a, b)
return a < b
end
local ipranges = {}
lines = {
"1.0.0.0/24",
"1.0.4.0/22",
}
for i=1, #lines do
local range = ipaddr:new(lines[i]:match('^%s*(%d+)%.(%d+)%.(%d+)%.(%d+)/(%d+)%s*$'))
if range then
insert(ipranges, range)
end
end
sort(ipranges, compare_networks)
local test = '1.0.0.1'
ip = ipaddr:new(test:match('^%s*(%d+)%.(%d+)%.(%d+)%.(%d+)'))
print(ip.netmask, ip.network, ip.broadcast, ip.str(ip))
for _, r in ipairs(ipranges) do
if r<ip and r:contains(ip) then
print(r.network, ip.network, r.broadcast)
else
print(false)
end
end