1
1

More than 3 years have passed since last update.

Golang - How to Get Local Mac and IP Address?

Last updated at Posted at 2019-11-18
    //----------------------
    // Get the local machine IP address
    // https://www.socketloop.com/tutorials/golang-get-local-ip-and-mac-address
    //----------------------

    addrs, err := net.InterfaceAddrs()

    if err != nil {
        fmt.Println(err)
    }

    var currentIP, currentNetworkHardwareName string

    for _, address := range addrs {

        // check the address type and if it is not a loopback the display it
        // = GET LOCAL IP ADDRESS
        if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
            if ipnet.IP.To4() != nil {
                fmt.Println("Current IP address : ", ipnet.IP.String())
                currentIP = ipnet.IP.String()
            }
        }
    }

    fmt.Println("------------------------------")
    fmt.Println("We want the interface name that has the current IP address")
    fmt.Println("MUST NOT be binded to 127.0.0.1 ")
    fmt.Println("------------------------------")

    // get all the system's or local machine's network interfaces

    interfaces, _ := net.Interfaces()
    for _, interf := range interfaces {

        if addrs, err := interf.Addrs(); err == nil {
            for index, addr := range addrs {
                fmt.Println("[", index, "]", interf.Name, ">", addr)

                // only interested in the name with current IP address
                if strings.Contains(addr.String(), currentIP) {
                    fmt.Println("Use name : ", interf.Name)
                    currentNetworkHardwareName = interf.Name
                }
            }
        }
    }

    fmt.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

    fmt.Println("Hardware name : ", name)
    fmt.Println("MAC address : ", macAddress)

    // verify if the MAC address can be parsed properly
    hwAddr, err := net.ParseMAC(macAddress.String())

    if err != nil {
        fmt.Println("No able to parse MAC address : ", err)
        os.Exit(-1)
    }

    fmt.Printf("Physical hardware address : %s \n", hwAddr.String())

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1