-
Notifications
You must be signed in to change notification settings - Fork 0
/
rtab.go
144 lines (127 loc) · 2.73 KB
/
rtab.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main
import (
"bytes"
"fmt"
"log"
"os/exec"
"strconv"
"strings"
)
func GetNetstatOutput() string {
netstat := exec.Command("netstat", "-rnf", "inet")
var out bytes.Buffer
netstat.Stdout = &out
err := netstat.Run()
if err != nil {
log.Fatal(err)
}
return out.String()
}
type NetstatLine struct {
destination string
gateway string
flags string
refs int
use int
netif string
expire int
seq int
}
type NetstatLines []*NetstatLine
func (m *NetstatLine) Display() string {
return fmt.Sprintf("%15s %8s %3d", m.destination, m.flags, m.seq)
}
func (m *NetstatLine) Route() string {
return fmt.Sprintf("%15s (%s)", m.gateway, m.netif)
}
func parse(line string, seq int) *NetstatLine {
parts := strings.Fields(line)
var expire int = -1
if len(parts) > 6 {
expire1, err := strconv.Atoi(parts[6])
if err == nil {
expire = expire1
}
}
refs, err := strconv.Atoi(parts[3])
if err != nil {
refs = -1
}
use, err := strconv.Atoi(parts[4])
if err != nil {
use = -1
}
retval := &NetstatLine{
destination: parts[0],
gateway: parts[1],
flags: parts[2],
refs: refs,
use: use,
netif: parts[5],
expire: expire,
seq: seq,
}
return retval
}
func Lines(data string) []*NetstatLine {
lines := strings.Split(data, "\n")
startRecording := false
retval := []*NetstatLine{}
for i := 0; i < len(lines); i++ {
if startRecording && len(lines[i]) > 0 {
retval = append(retval, parse(lines[i], i))
}
if strings.Contains(lines[i], "Destination") {
startRecording = true
}
}
return retval
}
func printGateway(gateway string) {
fmt.Println(" --> ", gateway)
fmt.Println("--------------------------------------------------------------")
fmt.Println("")
fmt.Println("")
}
func main() {
netstat := GetNetstatOutput()
lines := Lines(netstat)
gateways := make(map[string]NetstatLines)
for _, val := range lines {
route := val.Route()
if gateways[route] == nil {
gateways[route] = make(NetstatLines, 0)
}
gateways[route] = append(gateways[route], val)
}
fmt.Println("")
fmt.Println("")
for k, v := range gateways {
if !strings.Contains(k, ":") && !strings.Contains(k, "#") {
didPrint := 0
for _, v1 := range v {
if v1.destination != "default" {
fmt.Println("", v1.Display())
didPrint = didPrint + 1
}
}
if didPrint > 0 {
printGateway(k)
}
}
}
for k, v := range gateways {
if !strings.Contains(k, ":") && !strings.Contains(k, "#") {
didPrint := 0
for _, v1 := range v {
if v1.destination == "default" {
fmt.Println("", v1.Display())
didPrint = didPrint + 1
}
}
if didPrint > 0 {
printGateway(k)
}
}
}
}