now able to parse multiple csv files at once

This commit is contained in:
konrad 2018-11-28 21:56:16 +01:00
parent 9dcf6d9c6f
commit e2a5d3d6ca
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
1 changed files with 54 additions and 65 deletions

119
main.go
View File

@ -7,7 +7,8 @@ import (
"fmt"
"io/ioutil"
"log"
"os/exec"
"os"
"path/filepath"
"strconv"
"strings"
"time"
@ -21,14 +22,13 @@ type WifiClient struct {
Packets int64
}
const CSVDump = `dump.csv`
const CSVDumps = `/home/konrad/go/src/git.kolaente.de/konrad/wifi-statistics`
const TheClientCSVHeader = `Station MAC, First time seen, Last time seen, Power, # packets, BSSID, Probed ESSIDs`
const SecondsUntilInactive = 120
func main() {
clients := ParseCSVDump(CSVDump)
//clients := GetClientsFromBytes()
clients := ParseCSVDump(CSVDumps)
var activeClients int64
for _, c := range clients {
@ -41,74 +41,63 @@ func main() {
fmt.Println("Total Clients:", len(clients))
}
func GetClientsFromBytes() (clients []*WifiClient) {
// Tooo much hassle, just output a csv dump and parse that and delete it afterwards should be good enough
// -u <secs> für delay
argstr := []string{"-c", "airodump-ng wlp59s0 -u 9"}
command := exec.Command("/bin/bash", argstr...)
var buf bytes.Buffer
command.Stdout = &buf
if err := command.Start(); err != nil {
log.Fatal(err)
}
timer := time.AfterFunc(10 *time.Second, func() {
err := command.Process.Kill()
func ParseCSVDump(pathToDumps string) (clients []*WifiClient) {
err := filepath.Walk(pathToDumps, func(dumpPath string, info os.FileInfo, err error) error {
if err != nil {
panic(err)
return err
}
// Only csv files
if info.IsDir() || filepath.Ext(dumpPath) != ".csv" {
return nil
}
bs, err := ioutil.ReadFile(dumpPath)
if err != nil {
log.Fatal(err)
}
all := string(bs)
i := 0
i = strings.Index(all, TheClientCSVHeader)
arefun := all[i+len(TheClientCSVHeader)+1:]
arefun = strings.Replace(arefun, " ", "", -1)
scanner := bufio.NewScanner(strings.NewReader(arefun))
for scanner.Scan() {
r := csv.NewReader(bytes.NewReader(scanner.Bytes()))
record, err := r.Read()
if err != nil {
if err.Error() == "EOF" {
continue
}
log.Fatal(err)
}
power, err := strconv.ParseInt(record[3], 10, 64)
if err != nil {
log.Fatal(err)
}
packets, err := strconv.ParseInt(record[4], 10, 64)
if err != nil {
log.Fatal(err)
}
clients = append(clients, &WifiClient{
MACAdress: record[0],
FirstSeen: parseDateToUnix(record[1]),
LastSeen: parseDateToUnix(record[2]),
Power: power,
Packets: packets,
})
}
return nil
})
command.Wait()
timer.Stop()
ioutil.WriteFile("dump", buf.Bytes(), 0644)
return
}
func ParseCSVDump(pathToDump string) (clients []*WifiClient) {
bs, err := ioutil.ReadFile(pathToDump)
if err != nil {
log.Fatal(err)
}
all := string(bs)
i := 0
i = strings.Index(all, TheClientCSVHeader)
arefun := all[i+len(TheClientCSVHeader)+1:]
arefun = strings.Replace(arefun, " ", "", -1)
scanner := bufio.NewScanner(strings.NewReader(arefun))
for scanner.Scan() {
r := csv.NewReader(bytes.NewReader(scanner.Bytes()))
record, err := r.Read()
if err != nil {
if err.Error() == "EOF" {
continue
}
log.Fatal(err)
}
power, err := strconv.ParseInt(record[3], 10, 64)
if err != nil {
log.Fatal(err)
}
packets, err := strconv.ParseInt(record[4], 10, 64)
if err != nil {
log.Fatal(err)
}
clients = append(clients, &WifiClient{
MACAdress: record[0],
FirstSeen: parseDateToUnix(record[1]),
LastSeen: parseDateToUnix(record[2]),
Power: power,
Packets: packets,
})
}
return
}