2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2020-05-16 15:35:21 +08:00
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
//
|
|
|
|
|
|
|
|
package gipv4
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
)
|
|
|
|
|
2020-05-18 23:50:31 +08:00
|
|
|
// GetMac retrieves and returns the first mac address of current host.
|
|
|
|
func GetMac() (mac string, err error) {
|
|
|
|
macs, err := GetMacArray()
|
2020-05-16 15:35:21 +08:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if len(macs) > 0 {
|
|
|
|
return macs[0], nil
|
|
|
|
}
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2020-05-18 23:50:31 +08:00
|
|
|
// GetMacArray retrieves and returns all the mac address of current host.
|
|
|
|
func GetMacArray() (macs []string, err error) {
|
2020-05-16 15:35:21 +08:00
|
|
|
netInterfaces, err := net.Interfaces()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, netInterface := range netInterfaces {
|
|
|
|
macAddr := netInterface.HardwareAddr.String()
|
|
|
|
if len(macAddr) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
macs = append(macs, macAddr)
|
|
|
|
}
|
|
|
|
return macs, nil
|
|
|
|
}
|