Last active
January 25, 2021 09:10
-
-
Save thuydao/0bdf55de68351536a6732cdaf9e7c73a to your computer and use it in GitHub Desktop.
go_mac_adr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "bytes" | |
| "strconv" | |
| "net" | |
| ) | |
| func main() { | |
| print(macAdrr()) | |
| } | |
| func macAdrr() string { | |
| interfaces, err := net.Interfaces() | |
| if err != nil { | |
| return "" | |
| } | |
| for _, i := range interfaces { | |
| if i.Flags&net.FlagUp != 0 && bytes.Compare(i.HardwareAddr, nil) != 0 { | |
| // Skip locally administered addresses | |
| if i.HardwareAddr[0]&2 == 2 { | |
| continue | |
| } | |
| var mac uint64 | |
| for j, b := range i.HardwareAddr { | |
| if j >= 8 { | |
| break | |
| } | |
| mac <<= 8 | |
| mac += uint64(b) | |
| } | |
| return strconv.FormatUint(mac, 16) | |
| } | |
| } | |
| return "" | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment