mirror of
https://gitee.com/johng/gf
synced 2026-07-08 22:40:30 +08:00
49 lines
905 B
Go
49 lines
905 B
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"net"
|
|
)
|
|
|
|
func getMacAddrs() (macAddrs []string) {
|
|
netInterfaces, err := net.Interfaces()
|
|
if err != nil {
|
|
fmt.Printf("fail to get net interfaces: %v", err)
|
|
return macAddrs
|
|
}
|
|
|
|
for _, netInterface := range netInterfaces {
|
|
macAddr := netInterface.HardwareAddr.String()
|
|
if len(macAddr) == 0 {
|
|
continue
|
|
}
|
|
|
|
macAddrs = append(macAddrs, macAddr)
|
|
}
|
|
return macAddrs
|
|
}
|
|
|
|
func getIPs() (ips []string) {
|
|
|
|
interfaceAddr, err := net.InterfaceAddrs()
|
|
if err != nil {
|
|
fmt.Printf("fail to get net interface addrs: %v", err)
|
|
return ips
|
|
}
|
|
|
|
for _, address := range interfaceAddr {
|
|
ipNet, isValidIpNet := address.(*net.IPNet)
|
|
if isValidIpNet && !ipNet.IP.IsLoopback() {
|
|
if ipNet.IP.To4() != nil {
|
|
ips = append(ips, ipNet.IP.String())
|
|
}
|
|
}
|
|
}
|
|
return ips
|
|
}
|
|
|
|
func main() {
|
|
fmt.Printf("mac addrs: %q\n", getMacAddrs())
|
|
fmt.Printf("ips: %q\n", getIPs())
|
|
}
|