Last active
July 7, 2020 20:51
-
-
Save drygdryg/60dccaaa93f6387b4c468821df8ed671 to your computer and use it in GitHub Desktop.
Router Scan report files (CSV) merger
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from os import walkFiles, paramStr, paramCount, tryRemoveFile | |
import csvtools | |
from strutils import join, toLowerAscii | |
import strformat | |
proc input(prompt: string = ""): string = | |
stdout.write(prompt) | |
stdout.flushFile() | |
result = stdin.readLine() | |
type wifiRouter = object | |
ip: string | |
port: string | |
time: string | |
status: string | |
authorization: string | |
server: string | |
radioOff: string | |
hidden: string | |
bssid: string | |
essid: string | |
security: string | |
key: string | |
pin: string | |
lanIp: string | |
lanMask: string | |
wanIp: string | |
wanMask: string | |
wanGateway: string | |
dns: string | |
latitude: string | |
longitude: string | |
comments: string | |
var dest_filename: string | |
if paramCount() == 0: | |
dest_filename = "merged.csv" | |
else: | |
dest_filename = paramStr(1) | |
var walked_files: seq[string] = @[] | |
var routers: seq[wifiRouter] = @[] | |
var success: int = 0 | |
for filename in walkFiles("*.csv"): | |
if filename == dest_filename: | |
continue | |
walked_files.add(filename) | |
var n = 1'u16 | |
for router in csv[wifiRouter](path = filename, separator=';', skipHeader=true): | |
if (router.bssid == "") or (router.essid == ""): | |
echo(fmt"В файле {filename} не обработана запись {n}: пустое содержимое") | |
continue | |
routers.add(router) | |
n.inc() | |
success.inc() | |
if walked_files.len == 0: | |
echo("Ошибка: файлы не найдены") | |
quit() | |
var outf = open(dest_filename, fmAppend) | |
routers.writeToCsv(outf, separator=';', quoteAlways=true) | |
echo("Успешно обработано записей: ", success) | |
if input("Удалить обработанные файлы? (n/Y): ").toLowerAscii() != "n": | |
for file in walked_files: | |
discard tryRemoveFile(file) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment