-
Notifications
You must be signed in to change notification settings - Fork 0
/
nics_linux.go
188 lines (156 loc) · 4.64 KB
/
nics_linux.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
//go:build linux
// +build linux
/*
nics_linux.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 (
"bufio"
"encoding/binary"
"fmt"
"io"
"log"
"net"
"os"
"strconv"
"strings"
"github.com/olekukonko/tablewriter"
)
// adapted from: https://raw.githubusercontent.com/fiskeben/resolv/master/main.go
type Resolver struct {
Domains []string
Nameservers []string
Search []string
Sortlist []string
}
// Config reads /etc/resolv.conf and returns it as a Resolver
func Config() (Resolver, error) {
f, err := os.Open("/etc/resolv.conf")
if err != nil {
return Resolver{}, err
}
defer f.Close()
return parse(f)
}
func parse(f io.Reader) (Resolver, error) {
domains := make([]string, 0)
nameservers := make([]string, 0)
search := make([]string, 0)
sortlist := make([]string, 0)
scanner := bufio.NewScanner(f)
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "#") {
continue
}
parts := strings.Split(line, " ")
if len(parts) < 2 {
continue
}
kind := parts[0]
rest := parts[1:]
switch kind {
case "domain":
for _, d := range rest {
d := strings.TrimSpace(d)
if d != "" {
domains = append(domains, d)
}
}
case "nameserver":
n := strings.Join(rest, "")
n = strings.TrimSpace(n)
nameservers = append(nameservers, n)
case "search":
for _, s := range rest {
s := strings.TrimSpace(s)
if s != "" {
search = append(domains, s)
}
}
case "sortlist":
for _, s := range rest {
s := strings.TrimSpace(s)
if s != "" {
sortlist = append(domains, s)
}
}
}
}
return Resolver{
Domains: domains,
Nameservers: nameservers,
Search: search,
Sortlist: sortlist,
}, nil
}
func getGatewaysAndDHCP(brief bool) (map[string]string, error) {
return nil, nil
}
// adapted from: https://stackoverflow.com/a/40695315/452281
/* /proc/net/route file:
Iface Destination Gateway Flags RefCnt Use Metric Mask
eno1 00000000 C900A8C0 0003 0 0 100 00000000 0 00
eno1 0000A8C0 00000000 0001 0 0 100 00FFFFFF 0 00
*/
const (
file = "/proc/net/route"
line = 1 // line containing the gateway addr. (first line: 0)
sep = "\t" // field separator
field = 2 // field containing hex gateway address (first field: 0)
)
func getGWFromRouteTable() string {
file, err := os.Open(file)
if err != nil {
log.Fatal(err)
}
defer file.Close()
gateway := ""
scanner := bufio.NewScanner(file)
for scanner.Scan() {
// jump to line containing the agteway address
for i := 0; i < line; i++ {
scanner.Scan()
}
// get field containing gateway address
tokens := strings.Split(scanner.Text(), sep)
gatewayHex := "0x" + tokens[field]
// cast hex address to uint32
d, _ := strconv.ParseInt(gatewayHex, 0, 64)
d32 := uint32(d)
// make net.IP address from uint32
ipd32 := make(net.IP, 4)
binary.LittleEndian.PutUint32(ipd32, d32)
//fmt.Printf("%T --> %[1]v\n", ipd32)
// format net.IP to dotted ipV4 string
gateway = net.IP(ipd32).String()
//fmt.Printf("%T --> %[1]v\n", gateway)
// exit scanner
break
}
return gateway
}
func gatewayAndDNS(allIPv4, allIPv6 []string, brief bool) {
conf, err := Config()
if err != nil {
fmt.Println(err)
return
}
dns := make([]string, 5)
for i := range conf.Nameservers {
dns[i] = conf.Nameservers[i]
}
gateway := getGWFromRouteTable()
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()
}