-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
61 lines (47 loc) · 1.01 KB
/
main.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
package main
import (
"context"
"fmt"
"os"
"os/signal"
"time"
"github.com/hdiniz/wpa_supplicant-go"
)
func main() {
if len(os.Args) != 2 {
fmt.Println("usage: wifi-scan <wpa_supplicant control interface path>")
os.Exit(1)
}
ctrlIfacePath := os.Args[1]
ctrl, err := wpa_supplicant.Connect(ctrlIfacePath)
if err != nil {
fmt.Printf("failed to connect to wpa_supplicant: %s\n", err)
os.Exit(1)
}
defer ctrl.Close()
exitCh := make(chan os.Signal, 1)
signal.Notify(exitCh, os.Interrupt)
ctx, cancelCtx := context.WithTimeout(context.Background(), 60*time.Second)
go func() {
<-exitCh
cancelCtx()
time.Sleep(3 * time.Second)
os.Exit(1)
}()
res, err := ctrl.SendRequest(ctx, "SCAN")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
if res != "OK\n" {
fmt.Println("failed to request scan", res)
os.Exit(1)
}
time.Sleep(2 * time.Second) // give some time to scan
res, err = ctrl.SendRequest(ctx, "SCAN_RESULTS")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println(res)
}