-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.go
48 lines (42 loc) · 1.05 KB
/
utils.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
package goul
import (
"bytes"
"errors"
"fmt"
"runtime"
"strconv"
"github.com/google/gopacket/pcap"
)
// PrintDevices print out all interfaces on the system.
func PrintDevices() error {
devices, err := pcap.FindAllDevs()
if err != nil {
return err
}
if len(devices) == 0 {
return errors.New("NoDevices")
}
fmt.Println(`Your system has following device(s).
Use name of the device with '-d' flag for override default device 'eth0'.
(e.g. '-d bond0')
Devices:`)
for _, device := range devices {
fmt.Println(" *", device.Name)
for _, address := range device.Addresses {
fmt.Println(" - IP address: ", address.IP)
}
}
return nil
}
// GoID returns goroutine ID.
// https://blog.sgmansfield.com/2015/12/goroutine-ids/
// https://groups.google.com/d/msg/golang-nuts/Nt0hVV_nqHE/bwndAYvxAAAJ
// https://play.golang.org/p/OeEmT_CXyO
func GoID() uint64 {
b := make([]byte, 64)
b = b[:runtime.Stack(b, false)]
b = bytes.TrimPrefix(b, []byte("goroutine "))
b = b[:bytes.IndexByte(b, ' ')]
n, _ := strconv.ParseUint(string(b), 10, 64)
return n
}