Lua表过滤ip';s
我已将主机文件和防火墙日志读取到一个表中,并筛选出ipv4/6和本地Lua表过滤ip';s,lua,Lua,我已将主机文件和防火墙日志读取到一个表中,并筛选出ipv4/6和本地 主机重定向,我现在在处理ipv4/6条目时有点卡住了,我没有查看 对于实现我想要的更多方法的代码,这是ipv4表的一个示例: 所需的输出将是从新表中迭代得到的: 因此,脚本已经识别出只有2个不同的IP,并且它的put只有 字符串/表格上的相应条目(如果不存在),则此处为 我的目标是: 要消除重复的IP和重复的主机,请创建一个表,其中IP地址是键,值是以主机为键的子表 ips = {} for _,line in pairs(t
主机重定向,我现在在处理ipv4/6条目时有点卡住了,我没有查看
对于实现我想要的更多方法的代码,这是ipv4表的一个示例: 所需的输出将是从新表中迭代得到的: 因此,脚本已经识别出只有2个不同的IP,并且它的put只有
字符串/表格上的相应条目(如果不存在),则此处为
我的目标是:
要消除重复的IP和重复的主机,请创建一个表,其中IP地址是键,值是以主机为键的子表
ips = {}
for _,line in pairs(test) do
local ip, host = line:match('(%S+)%s+(%S+)')
if not ips[ip] then ips[ip] = {} end
ips[ip][host] = true
end
你会得到这样一张桌子:
ips = {
['170.83.210.219'] = {
['www.test.com'] = true,
['test.org'] = true,
['www.test.co.uk'] = true,
},
['170.83.300.219'] = {
['test1.org'] = true,
['www.test1.co.uk'] = true,
['170.83.300.812'] = true,
},
}
这看起来很奇怪——您可能更喜欢将主机列表作为数组(即1-N作为键,主机作为值而不是键)——但将主机存储为键是消除重复的非常有效的方法
这只是意味着,与对ip成对(ips[x])这样的主机进行迭代不同,您可以对ip成对(ips[x])进行迭代
如果希望结果表采用OP中提到的
t[ip]=“host[host…]”
格式,可以修改例程以将每个主机存储为键(用于防止重复)和数组元素(用于将列表处理为空格分隔的字符串)。然后,在通过一次数据折叠任何重复项后,再通过另一次创建主机字符串:
ips = {}
for i,v in pairs(test) do
local ip, host = v:match('(%S+)%s+(%S+)')
if not ips[ip] then ips[ip] = {} end
if not ips[ip][host] then
ips[ip][host] = true -- this is duplicate prevention
table.insert(ips[ip], host) -- this is for our concatenation later
end
end
for ip,hosts in pairs(ips) do
ips[ip] = table.concat(hosts, ' ')
end
其结果是一个如下所示的表:
ips = {
["170.83.210.219"] = "www.test.com www.test.co.uk test.org",
["170.83.300.219"] = "170.83.300.812 www.test1.co.uk test1.org",
}
旁注:
t={'a','b'}
生成与t={}t[1]='a't[2]=b
相同的表
ips = {
['170.83.210.219'] = {
['www.test.com'] = true,
['test.org'] = true,
['www.test.co.uk'] = true,
},
['170.83.300.219'] = {
['test1.org'] = true,
['www.test1.co.uk'] = true,
['170.83.300.812'] = true,
},
}
ips = {}
for i,v in pairs(test) do
local ip, host = v:match('(%S+)%s+(%S+)')
if not ips[ip] then ips[ip] = {} end
if not ips[ip][host] then
ips[ip][host] = true -- this is duplicate prevention
table.insert(ips[ip], host) -- this is for our concatenation later
end
end
for ip,hosts in pairs(ips) do
ips[ip] = table.concat(hosts, ' ')
end
ips = {
["170.83.210.219"] = "www.test.com www.test.co.uk test.org",
["170.83.300.219"] = "170.83.300.812 www.test1.co.uk test1.org",
}