ws-sta-monitor: Fix whitespace

Signed-off-by: matthew <stidmatt@gmail.com>
This commit is contained in:
matthew
2021-12-03 08:18:45 -08:00
parent b12ff32552
commit 5cb74c1690

View File

@@ -18,6 +18,7 @@ import traceback
from time import sleep from time import sleep
import websocket import websocket
import re import re
try: try:
import thread import thread
except ImportError: except ImportError:
@@ -27,25 +28,23 @@ if sys.version_info[0] != 3:
print("This script requires Python 3") print("This script requires Python 3")
exit() exit()
sys.path.append(os.path.join(os.path.abspath(__file__ + "../../../"))) sys.path.append(os.path.join(os.path.abspath(__file__ + "../../../")))
LFUtils = importlib.import_module("py-json.LANforge.LFUtils") LFUtils = importlib.import_module("py-json.LANforge.LFUtils")
cre = {
cre={ "phy": re.compile(r'^(1\.\d+):\s+(\S+)\s+\(phy', re.I),
"phy": re.compile(r'^(1\.\d+):\s+(\S+)\s+\(phy', re.I), "ifname": re.compile(r'(1\.\d+):\s+IFNAME=(\S+)\s+', re.I),
"ifname": re.compile(r'(1\.\d+):\s+IFNAME=(\S+)\s+', re.I), "port": re.compile(r'Port (\S+)', re.I),
"port": re.compile(r'Port (\S+)', re.I), "connected": re.compile(r'.*?CTRL-EVENT-CONNECTED - Connection to ([a-f0-9:]+) complete', re.I),
"connected": re.compile(r'.*?CTRL-EVENT-CONNECTED - Connection to ([a-f0-9:]+) complete', re.I), "associated": re.compile(r'^.*?Associated with ([a-f0-9:]+)$', re.I),
"associated": re.compile(r'^.*?Associated with ([a-f0-9:]+)$', re.I), "auth": re.compile(r'.*: auth ([a-f0-9:]+) -> ([a-f0-9:]+) status: 0: Successful', re.I),
"auth": re.compile(r'.*: auth ([a-f0-9:]+) -> ([a-f0-9:]+) status: 0: Successful', re.I), "authenticated": re.compile(r'.*?Authenticated with ([a-f0-9:]+)', re.I),
"authenticated": re.compile(r'.*?Authenticated with ([a-f0-9:]+)', re.I), "associating": re.compile(r'.*?Trying to associate with ([a-f0-9:]+)', re.I),
"associating": re.compile(r'.*?Trying to associate with ([a-f0-9:]+)', re.I), "authenticating": re.compile(r'.*?[>]SME: Trying to authenticate with ([a-f0-9:]+)', re.I),
"authenticating": re.compile(r'.*?[>]SME: Trying to authenticate with ([a-f0-9:]+)', re.I),
} }
ignore=[ ignore = [
": scan finished", ": scan finished",
": scan started", ": scan started",
": scan aborted: ", ": scan aborted: ",
@@ -68,13 +67,14 @@ ignore=[
] ]
rebank = { rebank = {
"ifname" : re.compile("IFNAME=(\S+)") "ifname": re.compile("IFNAME=(\S+)")
} }
websock = None websock = None
host = "localhost" host = "localhost"
base_url = None base_url = None
port = 8081 port = 8081
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def usage(): def usage():
print("""Example: __file__ --host 192.168.1.101 --port 8081\n""") print("""Example: __file__ --host 192.168.1.101 --port 8081\n""")
@@ -94,7 +94,7 @@ def main():
base_url = "unset" base_url = "unset"
try: try:
args = parser.parse_args() args = parser.parse_args()
if (args.host is None): if args.host is None:
host = "localhost" host = "localhost"
elif (type(args) is tuple) or (type(args) is list): elif (type(args) is tuple) or (type(args) is list):
host = args.host[0] host = args.host[0]
@@ -104,7 +104,7 @@ def main():
base_url = "ws://%s:%s" % (host, port) base_url = "ws://%s:%s" % (host, port)
except Exception as e: except Exception as e:
print("Exception: "+e) print("Exception: " + e)
logging.exception(e) logging.exception(e)
usage() usage()
exit(2) exit(2)
@@ -125,79 +125,74 @@ def sock_filter(wsock, text):
resource = None resource = None
for test in ignore: for test in ignore:
if (test in text): if test in text:
if (debug): if debug:
print (" ignoring ",text) print(" ignoring ", text)
return; return
try: try:
message = json.loads(text) message = json.loads(text)
except Exception as ex:
print ("Json Exception: ", repr(ex))
traceback.print_exc()
try:
# big generic filter for wifi-message or details keys # big generic filter for wifi-message or details keys
try: try:
if ("details" in message.keys()): if "details" in message.keys():
for test in ignore: for test in ignore:
if (test in message["details"]): if test in message["details"]:
return; return
except KeyError: except KeyError:
print("Message lacks key 'details'") print("Message lacks key 'details'")
try: try:
if ("wifi-event" in message.keys()): if "wifi-event" in message.keys():
for test in ignore: for test in ignore:
# print (" is ",test, " in ", message["wifi-event"]) # print (" is ",test, " in ", message["wifi-event"])
if (test in message["wifi-event"]): if test in message["wifi-event"]:
return; return
except KeyError: except KeyError:
print("Message lacks key 'wifi-event'") print("Message lacks key 'wifi-event'")
if (("time" in message.keys()) and ("timestamp" in message.keys())): if ("time" in message.keys()) and ("timestamp" in message.keys()):
return return
if ("name" in message.keys()): if "name" in message.keys():
station_name = message["name"] station_name = message["name"]
if ("resource" in message.keys()): if "resource" in message.keys():
resource = "1.", message["resource"] resource = "1.", message["resource"]
if ("event_type" in message.keys()): if "event_type" in message.keys():
match_result = cre["port"].match(message["details"]) match_result = cre["port"].match(message["details"])
if (match_result is not None): if match_result is not None:
station_name = match_result.group(1) station_name = match_result.group(1)
if (message["is_alert"]): if message["is_alert"]:
print("alert: ", message["details"]) print("alert: ", message["details"])
# LFUtils.debug_printer.pprint(message) # LFUtils.debug_printer.pprint(message)
return return
else: else:
# LFUtils.debug_printer.pprint(message) # LFUtils.debug_printer.pprint(message)
if (" IP change from " in message["details"]): if " IP change from " in message["details"]:
if (" to 0.0.0.0" in messsage["details"]): if " to 0.0.0.0" in message["details"]:
print("e: %s.%s lost IP address", [resource, station_name]) print("e: %s.%s lost IP address", [resource, station_name])
else: else:
print("e: %s.%s gained IP address", [resource, station_name]) print("e: %s.%s gained IP address", [resource, station_name])
if ("Link DOWN" in message["details"]): if "Link DOWN" in message["details"]:
return # duplicates alert return # duplicates alert
print("event: ", message["details"]) print("event: ", message["details"])
return return
if ("wifi-event" in message.keys()): if "wifi-event" in message.keys():
if ("CTRL-EVENT-CONNECTED" in message["wifi-event"]): if "CTRL-EVENT-CONNECTED" in message["wifi-event"]:
# redunant # redunant
return return
if (("CTRL-EVENT-CONNECTED - Connection to " in message["wifi-event"]) and ( if (("CTRL-EVENT-CONNECTED - Connection to " in message["wifi-event"]) and (
" complete" in message["wifi-event"])): " complete" in message["wifi-event"])):
return;
if ((": assoc " in message["wifi-event"]) and ("status: 0: Successful" in message["wifi-event"])):
return return
if ((station_name is None) or (resource is None)): if (": assoc " in message["wifi-event"]) and ("status: 0: Successful" in message["wifi-event"]):
return
if (station_name is None) or (resource is None):
try: try:
match_result = cre["phy"].match(message["wifi-event"]) match_result = cre["phy"].match(message["wifi-event"])
if (match_result is not None): if match_result is not None:
# LFUtils.debug_printer.pprint(match_result) # LFUtils.debug_printer.pprint(match_result)
# LFUtils.debug_printer.pprint(match_result.groups()) # LFUtils.debug_printer.pprint(match_result.groups())
resource = match_result.group(1) resource = match_result.group(1)
@@ -206,7 +201,7 @@ def sock_filter(wsock, text):
match_result = cre["ifname"].match(message["wifi-event"]) match_result = cre["ifname"].match(message["wifi-event"])
# LFUtils.debug_printer.pprint(match_result) # LFUtils.debug_printer.pprint(match_result)
# LFUtils.debug_printer.pprint(match_result.groups()) # LFUtils.debug_printer.pprint(match_result.groups())
if (match_result is not None): if match_result is not None:
resource = match_result.group(1) resource = match_result.group(1)
station_name = match_result.group(2) station_name = match_result.group(2)
else: else:
@@ -221,9 +216,9 @@ def sock_filter(wsock, text):
sleep(1) sleep(1)
# print ("Determined station name: as %s.%s"%(resource, station_name)) # print ("Determined station name: as %s.%s"%(resource, station_name))
if ((": auth ") and ("status: 0: Successful" in message["wifi-event"])): if ": auth " and ("status: 0: Successful" in message["wifi-event"]):
match_result = cre["auth"].match(message["wifi-event"]) match_result = cre["auth"].match(message["wifi-event"])
if (match_result and match_result.groups()): if match_result and match_result.groups():
bssid = match_result.group(1) bssid = match_result.group(1)
print("station %s.%s auth with %s" % (resource, station_name, bssid)) print("station %s.%s auth with %s" % (resource, station_name, bssid))
return return
@@ -231,9 +226,9 @@ def sock_filter(wsock, text):
print("station %s.%s auth with ??" % (resource, station_name)) print("station %s.%s auth with ??" % (resource, station_name))
LFUtils.debug_printer.pprint(match_result) LFUtils.debug_printer.pprint(match_result)
if ("Associated with " in message["wifi-event"]): if "Associated with " in message["wifi-event"]:
match_result = cre["associated"].match(message["wifi-event"]) match_result = cre["associated"].match(message["wifi-event"])
if (match_result and match_result.groups()): if match_result and match_result.groups():
bssid = match_result.group(1) bssid = match_result.group(1)
print("station %s.%s assocated with %s" % (resource, station_name, bssid)) print("station %s.%s assocated with %s" % (resource, station_name, bssid))
return return
@@ -241,9 +236,9 @@ def sock_filter(wsock, text):
print("station %s.%s assocated with ??" % (resource, station_name)) print("station %s.%s assocated with ??" % (resource, station_name))
LFUtils.debug_printer.pprint(match_result) LFUtils.debug_printer.pprint(match_result)
if (" - Connection to " in message["wifi-event"]): if " - Connection to " in message["wifi-event"]:
match_result = cre["connected"].match(message["wifi-event"]) match_result = cre["connected"].match(message["wifi-event"])
if (match_result and match_result.groups()): if match_result and match_result.groups():
bssid = match_result.group(1) bssid = match_result.group(1)
print("station %s.%s connected to %s" % (resource, station_name, bssid)) print("station %s.%s connected to %s" % (resource, station_name, bssid))
return return
@@ -251,14 +246,14 @@ def sock_filter(wsock, text):
print("station %s.%s connected to ??" % (resource, station_name)) print("station %s.%s connected to ??" % (resource, station_name))
LFUtils.debug_printer.pprint(match_result) LFUtils.debug_printer.pprint(match_result)
if ("disconnected" in message["wifi-event"]): if "disconnected" in message["wifi-event"]:
print("Station %s.%s down" % (resource, station_name)) print("Station %s.%s down" % (resource, station_name))
return return
if ("Trying to associate with " in message["wifi-event"]): if "Trying to associate with " in message["wifi-event"]:
match_result = cre["associating"].match(message["wifi-event"]) match_result = cre["associating"].match(message["wifi-event"])
if (match_result and match_result.groups()): if match_result and match_result.groups():
bssid = match_result.group(1) bssid = match_result.group(1)
print("station %s.%s associating with %s" % (resource, station_name, bssid)) print("station %s.%s associating with %s" % (resource, station_name, bssid))
return return
@@ -266,10 +261,10 @@ def sock_filter(wsock, text):
print("station %s.%s associating with ??" % (resource, station_name)) print("station %s.%s associating with ??" % (resource, station_name))
LFUtils.debug_printer.pprint(match_result) LFUtils.debug_printer.pprint(match_result)
if ("Trying to authenticate" in message["wifi-event"]): if "Trying to authenticate" in message["wifi-event"]:
match_result = cre["authenticating"].match(message["wifi-event"]) match_result = cre["authenticating"].match(message["wifi-event"])
if (match_result and match_result.groups()): if match_result and match_result.groups():
bssid = match_result.group(1) bssid = match_result.group(1)
print("station %s.%s authenticating with %s" % (resource, station_name, bssid)) print("station %s.%s authenticating with %s" % (resource, station_name, bssid))
return return
@@ -277,10 +272,10 @@ def sock_filter(wsock, text):
print("station %s.%s authenticating with ??" % (resource, station_name)) print("station %s.%s authenticating with ??" % (resource, station_name))
LFUtils.debug_printer.pprint(match_result) LFUtils.debug_printer.pprint(match_result)
if ("Authenticated" in message["wifi-event"]): if "Authenticated" in message["wifi-event"]:
match_result = cre["authenticed"].match(message["wifi-event"]) match_result = cre["authenticed"].match(message["wifi-event"])
LFUtils.debug_printer.pprint(match_result) LFUtils.debug_printer.pprint(match_result)
if (match_result and match_result.groups()): if match_result and match_result.groups():
bssid = match_result.group(1) bssid = match_result.group(1)
print("station %s.%s authenticated with %s" % (resource, station_name, bssid)) print("station %s.%s authenticated with %s" % (resource, station_name, bssid))
else: else:
@@ -291,6 +286,10 @@ def sock_filter(wsock, text):
print("\nUnhandled: ") print("\nUnhandled: ")
LFUtils.debug_printer.pprint(message) LFUtils.debug_printer.pprint(message)
except Exception as ex:
traceback.print_exc()
raise ("Json Exception: ", repr(ex))
except KeyError as kerr: except KeyError as kerr:
print("# ----- Bad Key: ----- ----- ----- ----- ----- ----- ----- ----- ----- -----") print("# ----- Bad Key: ----- ----- ----- ----- ----- ----- ----- ----- ----- -----")
print("input: ", text) print("input: ", text)
@@ -317,6 +316,7 @@ def sock_filter(wsock, text):
sleep(1) sleep(1)
return return
# ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- # ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -----
def m_error(wsock, err): def m_error(wsock, err):
print("# ----- Error: ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -----\n") print("# ----- Error: ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -----\n")
@@ -355,7 +355,6 @@ def start_websocket(uri, websock):
if __name__ == '__main__': if __name__ == '__main__':
main() main()
#### ####
#### ####
#### ####