mirror of
https://github.com/kairos-io/kairos-sdk.git
synced 2025-09-16 07:09:30 +00:00
🐛 Add missing GetInterfaceIP
Signed-off-by: Itxaka <itxaka.garcia@spectrocloud.com>
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"fmt"
|
||||
"gopkg.in/yaml.v3"
|
||||
"image"
|
||||
"net"
|
||||
"os"
|
||||
"os/exec"
|
||||
"os/signal"
|
||||
@@ -198,3 +199,31 @@ func ListToOutput(rels []string, output string) []string {
|
||||
return rels
|
||||
}
|
||||
}
|
||||
|
||||
func GetInterfaceIP(in string) string {
|
||||
ifaces, err := net.Interfaces()
|
||||
if err != nil {
|
||||
fmt.Println("failed getting system interfaces")
|
||||
return ""
|
||||
}
|
||||
for _, i := range ifaces {
|
||||
if i.Name == in {
|
||||
addrs, _ := i.Addrs()
|
||||
// handle err
|
||||
for _, addr := range addrs {
|
||||
var ip net.IP
|
||||
switch v := addr.(type) {
|
||||
case *net.IPNet:
|
||||
ip = v.IP
|
||||
case *net.IPAddr:
|
||||
ip = v.IP
|
||||
}
|
||||
if ip != nil {
|
||||
return ip.String()
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
Reference in New Issue
Block a user