now able to parse multiple csv files at once
This commit is contained in:
parent
9dcf6d9c6f
commit
e2a5d3d6ca
119
main.go
119
main.go
|
@ -7,7 +7,8 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"os/exec"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
@ -21,14 +22,13 @@ type WifiClient struct {
|
||||||
Packets int64
|
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 TheClientCSVHeader = `Station MAC, First time seen, Last time seen, Power, # packets, BSSID, Probed ESSIDs`
|
||||||
const SecondsUntilInactive = 120
|
const SecondsUntilInactive = 120
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
clients := ParseCSVDump(CSVDump)
|
clients := ParseCSVDump(CSVDumps)
|
||||||
//clients := GetClientsFromBytes()
|
|
||||||
|
|
||||||
var activeClients int64
|
var activeClients int64
|
||||||
for _, c := range clients {
|
for _, c := range clients {
|
||||||
|
@ -41,74 +41,63 @@ func main() {
|
||||||
fmt.Println("Total Clients:", len(clients))
|
fmt.Println("Total Clients:", len(clients))
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetClientsFromBytes() (clients []*WifiClient) {
|
func ParseCSVDump(pathToDumps string) (clients []*WifiClient) {
|
||||||
|
err := filepath.Walk(pathToDumps, func(dumpPath string, info os.FileInfo, err error) error {
|
||||||
// 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()
|
|
||||||
if err != nil {
|
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 {
|
if err != nil {
|
||||||
log.Fatal(err)
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue