From 15c00db6b3317d5c5d4581a5a8ffe519fd0d51c7 Mon Sep 17 00:00:00 2001 From: Jed Reynolds Date: Wed, 26 Feb 2020 17:14:49 -0800 Subject: [PATCH] added more parsing --- py-json/websocket-filter.py | 87 ++++++++++++++++++++++++++----------- 1 file changed, 61 insertions(+), 26 deletions(-) diff --git a/py-json/websocket-filter.py b/py-json/websocket-filter.py index ce4c5099..70cee82e 100755 --- a/py-json/websocket-filter.py +++ b/py-json/websocket-filter.py @@ -19,6 +19,7 @@ import logging import time from time import sleep import websocket +import re try: import thread except ImportError: @@ -28,29 +29,34 @@ import LANforge from LANforge import LFRequest from LANforge import LFUtils from LANforge.LFUtils import NA + +ignore=[ + "scan finished", + "scan started" + "CTRL-EVENT-SCAN-STARTED", + "-EVENT-SSID-TEMP-DISABLED", + "new station", + "del station", + "ping", + "deleted-alert", +] +interesting=[ + "Trying to authenticate", + "auth: timed out", + "link DOWN", + "link UP", + 'wifi-event' +] + +rebank = { + "ifname" : re.compile("IFNAME=(\S+)") +} # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - def main(): host = "ct524-debbie.jbr.candelatech.com" base_url = "ws://%s:8081"%host websock = None - ignore=( - "scan finished", - "scan started" - "CTRL-EVENT-SCAN-STARTED", - "CTRL-EVENT-SSID-TEMP-DISABLED", - "new station", - "delstation", - "ping", - ) - interesting=( - "Trying to authenticate", - "auth: timed out", - 'link DOWN', - 'link UP', - 'wifi-event' - ) - # open websocket start_websocket(base_url, websock) if (websock is not None): @@ -61,31 +67,60 @@ def main(): # ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- def sock_filter(wsock, text): - debug = 1 + global ignore + global interesting + global rebank + debug = 0 for test in ignore: - print (".") if (test in text): if (debug): - print ("ignoring ",text) + print (" ignoring ",text) return; try: message = json.loads(text) - #if (("time" in message) and ("timestamp" in message)): - # return + if (("time" in message) and ("timestamp" in message)): + return + if ("event_type" in message): + if (message["is_alert"]): + print ("alert: ", message["details"]); + return + else: + print ("event: ", message["details"]); + return + if ("wifi-event" in message): for test in ignore: if (test in message["wifi-event"]): + if (debug): + print (" ignoring ",text) return; - print (message["wifi-event"], "\n") + #group = rebank["ifname"].match(message["wifi-event"]).group() + #if (group): + # print ("IFname: ",group) + + if ("disconnected" in message["wifi-event"]): + print ("Station down") + return + if ("Trying to authenticate" in message["wifi-event"]): + print ("station authenticating") + return + print ("wifi-event: ", message["wifi-event"]) else: print ("\nUnhandled: ") LFUtils.debug_printer.pprint(message) - except Exception: - print ("# ----- Not JSON: ----- ----- ----- ----- ----- ----- ----- ----- ----- -----\n") + except json.JSONDecodeError as derr: + print ("# ----- Decode err: ----- ----- ----- ----- ----- ----- ----- ----- ----- -----") print (text) - print ("# ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -----\n") + print (derr) + print ("# ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -----") + return + except Exception as ex: + print ("# ----- Not JSON: ----- ----- ----- ----- ----- ----- ----- ----- ----- -----") + print (text) + print (ex) + print ("# ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -----") return # ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -----