-
Notifications
You must be signed in to change notification settings - Fork 53
/
net.go
48 lines (37 loc) · 1.02 KB
/
net.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 main
import (
"net"
"os/exec"
"strings"
)
func getNetAddress() (string, error) {
rawAddr, _ := getHostAddress()
addr := net.ParseIP(rawAddr)
rawMask, _ := getNetMask()
mask := net.IPMask(net.ParseIP(rawMask).To4())
return addr.Mask(mask).String(), nil
}
func getHostAddress() (string, error) {
addr, err := exec.Command("defaults", "read", "/Library/Preferences/SystemConfiguration/com.apple.vmnet.plist", "Shared_Net_Address").Output()
if err != nil {
return "192.168.64.1", err
}
return strings.TrimSpace(string(addr)), nil
}
func getNetMask() (string, error) {
mask, err := exec.Command("defaults", "read", "/Library/Preferences/SystemConfiguration/com.apple.vmnet.plist", "Shared_Net_Mask").Output()
if err != nil {
return "255.255.255.0", err
}
return strings.TrimSpace(string(mask)), nil
}
func getDomain(hostname string) string {
var domain string
lastDot := strings.LastIndex(hostname, ".")
if lastDot > -1 {
domain = hostname[lastDot+1:]
} else {
domain = hostname
}
return domain
}