Skip to content

Instantly share code, notes, and snippets.

@twsnmp
Created December 21, 2019 00:11
Show Gist options
  • Save twsnmp/051b28fec778cab249d07a82659581a7 to your computer and use it in GitHub Desktop.
Save twsnmp/051b28fec778cab249d07a82659581a7 to your computer and use it in GitHub Desktop.
OUI to Vendor Name Map
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
// OUI Map
// Download oui.txt from
// http://standards-oui.ieee.org/oui/oui.txt
// OUIMap : OUI to Name Map
type OUIMap struct {
Map map[string]string
}
// Open : Load OUI Data from file
func (oui *OUIMap) Open(path string) error {
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
oui.Map = make(map[string]string)
s := bufio.NewScanner(f)
for s.Scan() {
l := strings.TrimSpace(s.Text())
if len(l) < 1 {
continue
}
f := strings.Fields(l)
if len(f) < 4 || f[1] != "(base" {
continue
}
oui.Map[f[0]] = strings.Join(f[3:], " ")
}
return nil
}
// Find : Find Vendor Name from MAC Address
func (oui *OUIMap) Find(mac string) string {
mac = strings.TrimSpace(mac)
mac = strings.ReplaceAll(mac, ":", "")
mac = strings.ReplaceAll(mac, "-", "")
if len(mac) > 6 {
mac = strings.ToUpper(mac)
if n, ok := oui.Map[mac[:6]]; ok {
return n
}
}
return "Unknown"
}
func main() {
oui := &OUIMap{}
if err := oui.Open("oui.txt"); err != nil {
panic(err)
}
for id, name := range oui.Map {
fmt.Printf("%s=%s\n", id, name)
}
test := []string{"48-D7-05-B8-FF-5F", "4C-56-9D-00-CF-D9", "70:1C:e7:2F:9F:3B"}
for _, m := range test {
fmt.Printf("%s=%s\n", m, oui.Find(m))
}
}
@twsnmp
Copy link
Author

twsnmp commented Dec 21, 2019

IEEEに登録されたMACアドレスのOUI(ベンダーコード)とベンダー名をGO言語のMapにするサンプルです。
ouiのデータは、
http://standards-oui.ieee.org/oui/oui.txt
からダウンロードできます。
サンプルのように、MACアドレスからベンダー名を検索することに利用できます。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment