2016-06-02 20:40:30 +00:00
|
|
|
package gateway
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"os/exec"
|
|
|
|
)
|
|
|
|
|
|
|
|
func DiscoverGateway() (ip net.IP, err error) {
|
|
|
|
ip, err = discoverGatewayUsingRoute()
|
|
|
|
if err != nil {
|
|
|
|
ip, err = discoverGatewayUsingIp()
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func discoverGatewayUsingIp() (net.IP, error) {
|
2017-04-05 14:34:41 +00:00
|
|
|
routeCmd := exec.Command("ip", "route", "show")
|
2016-06-02 20:40:30 +00:00
|
|
|
output, err := routeCmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return parseLinuxIPRoute(output)
|
|
|
|
}
|
|
|
|
|
|
|
|
func discoverGatewayUsingRoute() (net.IP, error) {
|
2017-04-05 14:34:41 +00:00
|
|
|
routeCmd := exec.Command("route", "-n")
|
2016-06-02 20:40:30 +00:00
|
|
|
output, err := routeCmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return parseLinuxRoute(output)
|
|
|
|
}
|