-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnics.go
165 lines (146 loc) · 3.85 KB
/
nics.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
/*
Adopted from:
https://github.com/jftuga/nics
*/
package main
import (
"fmt"
"net"
"os"
"strconv"
"strings"
"github.com/olekukonko/tablewriter"
)
func isBriefEntry(ifaceName, macAddr, mtu, flags string, ipv4List, ipv6List []string, debug bool) bool {
if debug {
fmt.Println("isBriefEntry:", ifaceName)
}
if strings.Contains(flags, "loopback") {
if debug {
fmt.Println(" not_brief: loopback flag")
}
return false
}
if strings.HasPrefix(macAddr, "00:00:00:00:00:00") {
if debug {
fmt.Println(" not_brief: NULL macAddr")
}
return false
}
if 0 == len(ipv4List) {
if debug {
fmt.Println(" not_brief: no IP addresses")
}
return false
}
for _, ipv4 := range ipv4List {
if strings.HasPrefix(ipv4, "169.254.") {
if debug {
fmt.Println(" not_brief: self assigned:", ipv4)
}
return false
}
}
if debug {
fmt.Println(" is_brief: true")
}
return true
}
func extractIPAddrs(ifaceName string, allAddresses []net.Addr, brief bool) ([]string, []string) {
var allIPv4 []string
var allIPv6 []string
for _, netAddr := range allAddresses {
address := netAddr.String()
if strings.Contains(address, ":") {
allIPv6 = append(allIPv6, address)
} else {
allIPv4 = append(allIPv4, address)
}
}
return allIPv4, allIPv6
}
func getSpecificNic(adapter string) (string, error) {
adapters, err := net.Interfaces()
if err != nil {
return "", err
}
adapter = strings.ToLower(adapter)
for _, iface := range adapters {
allAddresses, err := iface.Addrs()
if err != nil {
return "", err
}
allIPv4, _ := extractIPAddrs(iface.Name, allAddresses, true)
if len(allIPv4) == 1 && strings.ToLower(iface.Name) == adapter {
for _, ipWithMask := range allIPv4 {
ip := strings.Split(ipWithMask, "/")
return ip[0], nil
}
}
}
return "", fmt.Errorf("Unknown adapter: '%s'. Use '-i' to list adapter names.", adapter)
}
func networkInterfaces(brief bool, debug bool) ([]string, []string, error) {
adapters, err := net.Interfaces()
if err != nil {
return nil, nil, err
}
table := tablewriter.NewWriter(os.Stdout)
table.SetAutoWrapText(false)
if brief {
table.SetHeader([]string{"Name", "IPv4", "Mac Address", "MTU", "Flags"})
} else {
table.SetHeader([]string{"Name", "IPv4", "IPv6", "Mac Address", "MTU", "Flags"})
}
var v4Addresses []string
var v6Addresses []string
for _, iface := range adapters {
allAddresses, err := iface.Addrs()
if err != nil {
return nil, nil, nil
}
allIPv4, allIPv6 := extractIPAddrs(iface.Name, allAddresses, brief)
if debug {
fmt.Println()
fmt.Println("---------------------")
fmt.Println(iface.Name, allAddresses)
fmt.Println("ipv4:", allIPv4)
fmt.Println("ipv6:", allIPv6)
}
ifaceName := strings.ToLower(iface.Name)
macAddr := iface.HardwareAddr.String()
mtu := strconv.Itoa(iface.MTU)
flags := iface.Flags.String()
if brief && isBriefEntry(ifaceName, macAddr, mtu, flags, allIPv4, allIPv6, debug) {
table.Append([]string{iface.Name, strings.Join(allIPv4, "\n"), macAddr, mtu, flags})
for _, ipWithMask := range allIPv4 {
ip := strings.Split(ipWithMask, "/")
v4Addresses = append(v4Addresses, ip[0])
}
continue
}
if !brief {
table.SetAutoWrapText(true)
table.SetRowLine(true)
table.Append([]string{ifaceName, strings.Join(allIPv4, "\n"), strings.Join(allIPv6, "\n"), macAddr, mtu, strings.Replace(flags, "|", "\n", -1)})
for _, ipWithMask := range allIPv4 {
ip := strings.Split(ipWithMask, "/")
v4Addresses = append(v4Addresses, ip[0])
}
for _, ipWithMask := range allIPv6 {
ip := strings.Split(ipWithMask, "/")
v6Addresses = append(v6Addresses, ip[0])
}
}
}
table.Render()
return v4Addresses, v6Addresses, err
}
func nics() {
argsAllDetails := false
argsDebug := false
_, _, err := networkInterfaces(!(argsAllDetails), argsDebug)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
}
}