mirror of
https://github.com/edgebox-iot/edgeboxctl.git
synced 2026-09-24 05:41:43 +02:00
Added utils.getIP function, set to run as a 60 tick scheduled task
This commit is contained in:
@@ -3,6 +3,9 @@ package system
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/edgebox-iot/edgeboxctl/internal/utils"
|
||||
|
||||
"github.com/shirou/gopsutil/host"
|
||||
)
|
||||
@@ -21,3 +24,29 @@ func GetUptimeFormatted() string {
|
||||
minutes := ((uptime - (days * 60 * 60 * 24)) - (hours * 60 * 60)) / 60
|
||||
return fmt.Sprintf("%d days, %d hours, %d minutes", days, hours, minutes)
|
||||
}
|
||||
|
||||
func GetIP() string {
|
||||
ip := ""
|
||||
|
||||
// Trying to find a valid IP (For direct connection, not tunneled)
|
||||
ethResult := utils.ExecAndGetLines("/", "ip", []string{"-o", "-4", "addr", "list", "eth0"})
|
||||
for ethResult.Scan() {
|
||||
adapterRawInfo := strings.Fields(ethResult.Text())
|
||||
if ip == "" {
|
||||
ip = strings.Split(adapterRawInfo[3], "/")[0]
|
||||
}
|
||||
}
|
||||
|
||||
// If no IP was found yet, try wlan0
|
||||
if ip == "" {
|
||||
wlanResult := utils.ExecAndGetLines("/", "ip", []string{"-o", "-4", "addr", "list", "wlan0"})
|
||||
for wlanResult.Scan() {
|
||||
adapterRawInfo := strings.Fields(wlanResult.Text())
|
||||
if ip == "" {
|
||||
ip = strings.Split(adapterRawInfo[3], "/")[0]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ip
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user