forked from cloudfoundry/bosh-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes_searcher_unix.go
49 lines (37 loc) · 1.02 KB
/
routes_searcher_unix.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
48
49
// +build !windows
package net
import (
"strings"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshsys "github.com/cloudfoundry/bosh-utils/system"
)
// cmdRoutesSearcher uses `route -n` command to list routes
// which routes in a same format on Ubuntu and CentOS
type cmdRoutesSearcher struct {
runner boshsys.CmdRunner
}
func NewRoutesSearcher(runner boshsys.CmdRunner) RoutesSearcher {
return cmdRoutesSearcher{runner}
}
func (s cmdRoutesSearcher) SearchRoutes() ([]Route, error) {
var routes []Route
stdout, _, _, err := s.runner.RunCommandQuietly("route", "-n")
if err != nil {
return routes, bosherr.WrapError(err, "Running route")
}
for i, routeEntry := range strings.Split(stdout, "\n") {
if i < 2 { // first two lines are informational
continue
}
if routeEntry == "" {
continue
}
routeFields := strings.Fields(routeEntry)
routes = append(routes, Route{
Destination: routeFields[0],
Gateway: routeFields[1],
InterfaceName: routeFields[7],
})
}
return routes, nil
}