mirror of
https://github.com/gravitl/netmaker.git
synced 2025-10-07 17:51:56 +08:00
adding gateway routes for peers
This commit is contained in:
@@ -8,6 +8,32 @@ import (
|
||||
"github.com/gravitl/netmaker/netclient/ncutils"
|
||||
)
|
||||
|
||||
// GetDefaultRoute - Gets the default route (ip and interface) on a freebsd machine
|
||||
func GetDefaultRoute() (string, string, error) {
|
||||
var ipaddr string
|
||||
var iface string
|
||||
var err error
|
||||
|
||||
output, err := ncutils.RunCmd("route show default", true)
|
||||
if err != nil {
|
||||
return ipaddr, iface, err
|
||||
}
|
||||
outFormatted := strings.ReplaceAll(output, "\n", "")
|
||||
if !strings.Contains(outFormatted, "default") && !strings.Contains(outFormatted, "interface:") {
|
||||
return ipaddr, iface, fmt.Errorf("could not find default gateway")
|
||||
}
|
||||
outputSlice := strings.Split(string(outFormatted), " ")
|
||||
for i, outString := range outputSlice {
|
||||
if outString == "gateway:" {
|
||||
ipaddr = outputSlice[i+1]
|
||||
}
|
||||
if outString == "interface:" {
|
||||
iface = outputSlice[i+1]
|
||||
}
|
||||
}
|
||||
return ipaddr, iface, err
|
||||
}
|
||||
|
||||
func setRoute(iface string, addr *net.IPNet, address string) error {
|
||||
var err error
|
||||
_, _ = ncutils.RunCmd("route add -net "+addr.String()+" -interface "+iface, false)
|
||||
|
Reference in New Issue
Block a user