Skip to content

Commit

Permalink
Process ifconfig output
Browse files Browse the repository at this point in the history
  • Loading branch information
gonzoua committed May 9, 2020
0 parents commit 3aaa8ea
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions shyfi.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// +build ignore

package main

import (
"strings"
"fmt"
"bytes"
"os/exec"
)

type Network struct {
ssid, bssid string
}

func listScan(iface string) []Network {
cmd := exec.Command("ifconfig", "-v", iface, "list", "scan")
cmdOutput := &bytes.Buffer{}
cmd.Stdout = cmdOutput
err := cmd.Run()
if err != nil {
return nil
}
output := string(cmdOutput.Bytes())
lines := strings.Split(output, "\n")
if len(lines) < 1 {
return nil
}
header := lines[0]
lines = lines[1:]
ssidEnd := strings.Index(header, "BSSID") - 1
if ssidEnd < 0 {
return nil
}

result := []Network{}
for _, line := range lines {
if len(line) < ssidEnd + 1 {
continue
}
ssid := line[:ssidEnd]
ssid = strings.Trim(ssid, " ")
bssid := line[ssidEnd + 1:ssidEnd + 18]
network := Network {ssid: ssid, bssid: bssid}
result = append(result, network)
}
return result
}

func main() {
networks := listScan("wlan0")
fmt.Printf("Total networks: %d\n", len(networks))
for _, net := range networks {
fmt.Printf("===> [%s] %s\n", net.bssid, net.ssid)
}
}

0 comments on commit 3aaa8ea

Please sign in to comment.