-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathwifi-name.go
83 lines (66 loc) · 1.42 KB
/
wifi-name.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
package wifiname
import (
"io/ioutil"
"os/exec"
"regexp"
"runtime"
"strings"
)
const osxCmd = "/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport"
const osxArgs = "-I"
const linuxCmd = "iwgetid"
const linuxArgs = "--raw"
func WifiName() string {
platform := runtime.GOOS
if platform == "darwin" {
return forOSX()
} else if platform == "win32" {
// TODO for Windows
return ""
} else {
// TODO for Linux
return forLinux()
}
}
func forLinux() string {
cmd := exec.Command(linuxCmd, linuxArgs)
stdout, err := cmd.StdoutPipe()
panicIf(err)
// start the command after having set up the pipe
if err := cmd.Start(); err != nil {
panic(err)
}
defer cmd.Wait()
var str string
if b, err := ioutil.ReadAll(stdout); err == nil {
str += (string(b) + "\n")
}
name := strings.Replace(str, "\n", "", -1)
return name
}
func forOSX() string {
cmd := exec.Command(osxCmd, osxArgs)
stdout, err := cmd.StdoutPipe()
panicIf(err)
// start the command after having set up the pipe
if err := cmd.Start(); err != nil {
panic(err)
}
defer cmd.Wait()
var str string
if b, err := ioutil.ReadAll(stdout); err == nil {
str += (string(b) + "\n")
}
r := regexp.MustCompile(`s*SSID: (.+)s*`)
name := r.FindAllStringSubmatch(str, -1)
if len(name) <= 1 {
return "Could not get SSID"
} else {
return name[1][1]
}
}
func panicIf(err error) {
if err != nil {
panic(err)
}
}