Page MenuHomePhabricator

localMac_addr.go
No OneTemporary

localMac_addr.go

package main
import (
"fmt"
"net"
"strings"
"os"
)
func main() {
// localMacAddr, err := net.()
// if err != nil{
// panic(err)
// }
// fmt.Println("Local MAC addr:",localMacAddr)
//
//
var currentIP, currentNetworkHardwareName string
currentIP = "134.197.41.183/22"
interfaces, _ := net.Interfaces()
for _, interf := range interfaces {
if addrs, err := interf.Addrs(); err == nil {
for index, addr := range addrs {
if addr.String() != "127.0.0.1/8" {
log.Println("[", index, "]", interf.Name, ">", addr)
// only interested in the name with current IP address
if strings.Contains(addr.String(), currentIP) {
log.Println("Use name : ", interf.Name)
currentNetworkHardwareName = interf.Name
}
}
}
}
}
log.Println("-------------------------------------------")
// extract the hardware information base on the interface name
// capture above
netInterface, err := net.InterfaceByName(currentNetworkHardwareName)
if err != nil {
fmt.Println(err)
}
name := netInterface.Name
macAddress := netInterface.HardwareAddr
log.Println("Hardware name : ", name)
log.Println("MAC address : ", macAddress)
// verify if the MAC address can be parsed properly
hwAddr, err := net.ParseMAC(macAddress.String())
if err != nil {
log.Println("No able to parse MAC address : ", err)
os.Exit(-1)
}
log.Println("Physical hardware address : %s \n", hwAddr.String())
}
// net.Interfaces() // returns an array of interfaces
// net.InterfaceByName(oneOfTheReturnedInterfaces)
// ^.HardwareAddr

File Metadata

Mime Type
text/x-c
Expires
Tue, Feb 25, 11:16 AM (1 d, 20 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
15169
Default Alt Text
localMac_addr.go (2 KB)

Event Timeline