mirror of
https://github.com/siderolabs/talos.git
synced 2025-08-25 08:31:13 +02:00
25 lines
440 B
Go
25 lines
440 B
Go
package net
|
|
|
|
import (
|
|
"fmt"
|
|
"net"
|
|
)
|
|
|
|
// IP finds and returns the first non-loopback interface of the current machine.
|
|
func IP() (ip net.IP, err error) {
|
|
addrs, err := net.InterfaceAddrs()
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
for _, a := range addrs {
|
|
if ipnet, ok := a.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
|
|
if ipnet.IP.To4() != nil {
|
|
return ipnet.IP, nil
|
|
}
|
|
}
|
|
}
|
|
|
|
return nil, fmt.Errorf("could not discover IP address")
|
|
}
|