Skip to content

Instantly share code, notes, and snippets.

@gb-swatanabe
Last active December 22, 2015 08:49
Show Gist options
  • Save gb-swatanabe/6447910 to your computer and use it in GitHub Desktop.
Save gb-swatanabe/6447910 to your computer and use it in GitHub Desktop.
MACアドレスからベンダコードを検索するコード(oui.txtを利用)
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
# oui.txtの入手方法:
# curl -O http://standards.ieee.org/develop/regauth/oui/oui.txt
# OUIデータベースの読み込み
def oui_store filename,o
# ファイルから読み込む
File.read( filename ).split(/\n/).grep(/\(hex\)/).each{|line|
code,note = line.sub(/^\s+/,'').split(/\s+/,3).values_at(0,2)
o[code.downcase.gsub(/[^\da-f]/,'')] = note
}
# 例外
o.merge!({
"080027" => "VirtualBox Guest",
"525400" => "QEMU/KVM Guest",
})
o.keys.length
end
# MAC -> OUIコーポレートコード
def ouiquery mac,db
db[ mac.downcase.gsub(/[^\da-f]/,'').slice(0,6) ]
end
#----#----#----#----#----#----#----#----#----#----#----#----#----#----#----#----#----#----#
# OUIデータベースを読み込み
ouidb = Hash.new
file = 'oui.txt'
macaddress = ['8c:2d:aa:5e:05:1b',
'00:30:48:B1:BF:5E',
'52-54-00-e7-e9-e4',
]
printf "Reading from: %s ...", file
r = oui_store file,ouidb
printf "done. %d entries.\n\n", r
# 検索
macaddress.each{ |mac|
printf " %s -> %s\n", mac, ouiquery( mac, ouidb )
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment