forked from jackpal/gateway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgateway_linux.go
47 lines (39 loc) · 947 Bytes
/
gateway_linux.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package gateway
import (
"net"
"os/exec"
)
func DiscoverGateway() (ip net.IP, err error) {
ip, err = discoverGatewayUsingRoute()
if err != nil {
ip, err = discoverGatewayUsingIpRouteShow()
}
if err != nil {
ip, err = discoverGatewayUsingIpRouteGet()
}
return
}
func discoverGatewayUsingIpRouteShow() (net.IP, error) {
routeCmd := exec.Command("ip", "route", "show")
output, err := routeCmd.CombinedOutput()
if err != nil {
return nil, err
}
return parseLinuxIPRouteShow(output)
}
func discoverGatewayUsingIpRouteGet() (net.IP, error) {
routeCmd := exec.Command("ip", "route", "get", "8.8.8.8")
output, err := routeCmd.CombinedOutput()
if err != nil {
return nil, err
}
return parseLinuxIPRouteGet(output)
}
func discoverGatewayUsingRoute() (net.IP, error) {
routeCmd := exec.Command("route", "-n")
output, err := routeCmd.CombinedOutput()
if err != nil {
return nil, err
}
return parseLinuxRoute(output)
}