有没有一种有效的方法来查询macaddress.io json Mac数据库,使用jq来查找网络汽车制造商。该数据库可在https://macaddress.io/database-download上免费获得(请选择json +下载
"oui": "00:50:C2:73:E",
"isPrivate": false,
"companyName": "Quantec Networks GmbH",
"companyAddress": "Rieselwiese 1 Vienenburg Niedersachsen 38690 DE",
"countryCode": "DE",
"assignmentBlockSize": "IAB",
"dateCreated": "2007-05-14",
"dateUpdated": "2015-08-29"
}
对于DeLL计算机,我可以这样查询:cat macaddress.io-db.json |jq 'select(.oui|test("14:18:77"))'
不幸的是,对于上面的记录,有多个公司名称匹配00:50:C2,您需要给出更多的数字才能有唯一的正确答案。
丑陋的方法是在整个MAC地址上循环,删除一个数字,直到查询成功。
因此,对于一台DeLL计算机,这将循环6次才能得到结果:让我们以14:18:77:34:23:12MAC为例:
1st query: 14:18:77:34:23:1 => no answer
2nd query: 14:18:77:34:23 => no answer
3rd query: 14:18:77:34:2 => no answer
4th query: 14:18:77:34 => no answer
5th query: 14:18:77:3 => no answer
6th query: 14:18:77 => we get the DeLL record:
{
"oui": "14:18:77",
"isPrivate": false,
"companyName": "Dell Inc",
"companyAddress": "One Dell way Round Rock 78682 US",
"countryCode": "US",
"assignmentBlockSize": "MA-L",
"dateCreated": "2015-06-13",
"dateUpdated": "2019-07-01"
}
我想知道是否有一种更聪明的方法,可以在完全MAC的情况下返回最佳匹配。
例如: 14:18:77 :34:23:12MAC将匹配14:18:77记录(DeLL)
和00:50:C2:73:E3:27 MAC将匹配00:50:C2:73:E记录,而不是00:50:C2:73:F记录。
有什么想法/提示/jq命令来完成上面的“软/智能”匹配吗?
(目标是,在给定完整的MAC (大写或小写)的情况下,检索companyName字段,如果未找到,则检索“未知”(或"")。)
请注意,oui字段是唯一的,并且没有子集(如果存在XX:YY:ZZ:TT,则不能有XX:YY:ZZ (但可以有XX:YY:ZZ:UU))。知道这一点,意味着最长的匹配是唯一的(单个记录)。
发布于 2020-01-11 11:55:45
合理有效的解决方案的关键是从使用inputs
和jq的-n命令行选项开始收集合理的候选对象。考虑到这一点,我们可以这样写:
# Winnow recursively
def winnow($s; $i):
if ($s|length) < $i then . # no further winnowing
else $s[0:$i] as $ss
| map(select(.oui | startswith($ss))) as $result
| if ($result|length) == 0 then . # prior
elif ($result|length) == 1 then $result
else $result | winnow($s; $i+1)
end
end;
def bestMatch($s):
$s[0:1] as $first
| [inputs | select(.oui | startswith($first))]
| (select(length>0) | winnow($s; 2)) // "Unknown"
| if type == "array" and length == 1 then .[0] else . end
;
示例
bestMatch("14:18:77:34:23:12")
生成.oui ==为“14:18:77”的JSON对象;
bestMatch("00:50:C2:73:E3:27")
生成.oui ==为"00:50:C2:73:E“的JSON对象。
https://stackoverflow.com/questions/59685626
复制相似问题