-
Notifications
You must be signed in to change notification settings - Fork 13
/
scan.go
147 lines (137 loc) · 3.24 KB
/
scan.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
package wifiscan
import (
"bytes"
"fmt"
"os/exec"
"runtime"
"strconv"
"strings"
"time"
log "github.com/schollz/logger"
)
var TimeLimit = 10 * time.Second
func init() {
log.SetLevel("info")
}
// Scan will scan the optional interface for wifi access points
func Scan(wifiInterface ...string) (wifilist []Wifi, err error) {
if runtime.GOOS == "linux" && (len(wifiInterface) == 0 || wifiInterface[0] == "") {
var interfaces []string
interfaces, err = getInterfacesLinux()
if err != nil {
log.Debug(err)
return
}
for _, in := range interfaces {
var w []Wifi
w, err = scan(in)
if len(w) > 0 {
wifilist = append(wifilist, w...)
}
}
if len(wifilist) > 0 {
err = nil
wifimap := make(map[string]Wifi)
for _, w := range wifilist {
wifimap[w.BSSID] = w
}
i := 0
for _, w := range wifimap {
wifilist[i] = w
i++
}
wifilist = wifilist[:i]
}
return
}
return scan(wifiInterface...)
}
func scan(wifiInterface ...string) (wifilist []Wifi, err error) {
command := ""
os := ""
switch runtime.GOOS {
case "windows":
os = "windows"
command = "netsh.exe wlan show networks mode=Bssid"
_, _, errRun := runCommand(TimeLimit, "netsh interface set interface name=Wi-Fi admin=disabled")
if errRun != nil {
log.Debug(errRun)
}
_, _, errRun = runCommand(TimeLimit, "netsh interface set interface name=Wi-Fi admin=enabled")
if errRun != nil {
log.Debug(errRun)
}
time.Sleep(3 * time.Second)
case "darwin":
os = "darwin"
command = "/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -s"
default:
os = "linux"
command = "iwlist wlan0 scan"
if len(wifiInterface) > 0 && len(wifiInterface[0]) > 0 {
command = fmt.Sprintf("iwlist %s scan", wifiInterface[0])
}
}
stdout, _, err := runCommand(TimeLimit, command)
if err != nil {
log.Debugf("error on '%s': %s", command, err)
log.Debug(stdout)
return
}
wifilist, err = Parse(stdout, os)
return
}
func runCommand(tDuration time.Duration, commands string) (stdout, stderr string, err error) {
log.Debugf("running '%s' for %s", commands, tDuration)
command := strings.Fields(commands)
cmd := exec.Command(command[0])
if len(command) > 0 {
cmd = exec.Command(command[0], command[1:]...)
}
var outb, errb bytes.Buffer
cmd.Stdout = &outb
cmd.Stderr = &errb
err = cmd.Start()
if err != nil {
return
}
done := make(chan error, 1)
go func() {
done <- cmd.Wait()
}()
select {
case <-time.After(tDuration):
err = cmd.Process.Kill()
case err = <-done:
stdout = outb.String()
stderr = errb.String()
}
return
}
func getInterfacesLinux() (interfaces []string, err error) {
stdout, _, err := runCommand(TimeLimit, "ip address")
if err != nil {
return
}
return getInteracesFromString(stdout)
}
func getInteracesFromString(s string) (interfaces []string, err error) {
for _, line := range strings.Split(s, "\n") {
if !strings.Contains(line, "BROADCAST") {
continue
}
cols := strings.Split(line, ":")
if len(cols) < 3 {
continue
}
_, errConvert := strconv.Atoi(cols[0])
if errConvert != nil {
continue
}
if strings.Contains(cols[1], "@") || strings.Contains(cols[1], "docker") {
continue
}
interfaces = append(interfaces, strings.TrimSpace(cols[1]))
}
return
}