-
Notifications
You must be signed in to change notification settings - Fork 0
/
nics_darwin.go
129 lines (107 loc) · 3.59 KB
/
nics_darwin.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
//go:build darwin
// +build darwin
/*
nics_darwin.go
-John Taylor
2019-08-03
Display information about Network Interface Cards (NICs)
MIT License; Copyright (c) 2019 John Taylor
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package main
import (
"github.com/olekukonko/tablewriter"
"golang.org/x/net/route"
"os"
"os/exec"
"strconv"
"strings"
)
func getGatewaysAndDHCP(brief bool) (map[string]string, error) {
return nil, nil
}
// adopted from: https://stackoverflow.com/a/31221013/452281
func convert(b []byte) string {
s := make([]string, len(b))
for i := range b {
s[i] = strconv.Itoa(int(b[i]))
}
return strings.Join(s, ".")
}
// extract the nameservers from this cmd-line output: scutil --dns
func parseOutput(output string) []string {
var dns = []string{"N/A", "N/A"}
allLines := strings.Split(output, "\n")
i := 0
resolverCount := 0
for _, line := range allLines {
//fmt.Println("rc:", resolverCount, " i:", i, " dns:", dns, " line:", line)
if strings.Contains(line, "resolver #") {
resolverCount += 1
if resolverCount >= 2 && i > 0 {
break
}
}
if strings.Contains(line, "nameserver[") {
clean := strings.TrimSpace(line)
slots := strings.Split(clean, " ")
if len(slots) == 3 {
dns[i] = slots[2]
i += 1
if i == 2 {
break
}
}
}
}
return dns
}
func getMacOSDNS() []string {
var dns = []string{"N/A", "N/A"}
dnsCmd := exec.Command("/usr/sbin/scutil", "--dns")
output, err := dnsCmd.CombinedOutput()
if err != nil {
return dns
}
dns = parseOutput(string(output))
return dns
}
// adopted from: https://gist.github.com/abimaelmartell/dcbbff464dc0778165b2dcc5092f90e6
func getMacOSDefaultGateway() string {
var defaultRoute = [4]byte{0, 0, 0, 0}
rib, _ := route.FetchRIB(0, route.RIBTypeRoute, 0)
messages, err := route.ParseRIB(route.RIBTypeRoute, rib)
if err != nil {
return "N/A"
}
for _, message := range messages {
route_message := message.(*route.RouteMessage)
addresses := route_message.Addrs
var destination, gateway *route.Inet4Addr
ok := false
if destination, ok = addresses[0].(*route.Inet4Addr); !ok {
continue
}
if gateway, ok = addresses[1].(*route.Inet4Addr); !ok {
continue
}
if destination == nil || gateway == nil {
continue
}
if destination.IP == defaultRoute {
return convert(gateway.IP[:])
}
}
return "N/A"
}
func gatewayAndDNS(allIPv4, allIPv6 []string, brief bool) {
gateway := getMacOSDefaultGateway()
dns := getMacOSDNS()
table := tablewriter.NewWriter(os.Stdout)
table.SetAutoWrapText(false)
table.SetHeader([]string{"Gateway", "DNS 1", "DNS 2"})
table.Append([]string{gateway, dns[0], dns[1]})
table.Render()
}