Skip to content

Commit

Permalink
Lookup IP on $PATH to support unusual locations
Browse files Browse the repository at this point in the history
Fixes #12

Manually tested on Ubuntu 17.04 (`/sbin/ip`) and CoreOS 1409.2.0
(`/usr/bin/ip`).
  • Loading branch information
schmichael committed Jun 22, 2017
1 parent 2d10d7c commit b44a235
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions route_info_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,29 @@ import (
"os/exec"
)

var cmds map[string][]string = map[string][]string{
"ip": {"/sbin/ip", "route"},
}

type routeInfo struct {
cmds map[string][]string
}

// NewRouteInfo returns a Linux-specific implementation of the RouteInfo
// interface.
func NewRouteInfo() (routeInfo, error) {
// CoreOS Container Linux moved ip to /usr/bin/ip, so look it up on
// $PATH and fallback to /sbin/ip on error.
path, _ := exec.LookPath("ip")
if path == "" {
path = "/sbin/ip"
}

return routeInfo{
cmds: cmds,
cmds: map[string][]string{"ip": {path, "route"}},
}, nil
}

// GetDefaultInterfaceName returns the interface name attached to the default
// route on the default interface.
func (ri routeInfo) GetDefaultInterfaceName() (string, error) {
out, err := exec.Command(cmds["ip"][0], cmds["ip"][1:]...).Output()
out, err := exec.Command(ri.cmds["ip"][0], ri.cmds["ip"][1:]...).Output()
if err != nil {
return "", err
}
Expand Down

0 comments on commit b44a235

Please sign in to comment.