|
@@ -1,6 +1,12 @@
|
1
|
|
-import bottombar as bb
|
2
|
1
|
import argparse
|
3
|
|
-from console_bottombar import BottomBar
|
|
2
|
+try:
|
|
3
|
+ import bottombar as bb
|
|
4
|
+except ModuleNotFoundError:
|
|
5
|
+ bb = None
|
|
6
|
+try:
|
|
7
|
+ from console_bottombar import BottomBar
|
|
8
|
+except ModuleNotFoundError:
|
|
9
|
+ BottomBar = None
|
4
|
10
|
import getpass
|
5
|
11
|
import json
|
6
|
12
|
import mqtt
|
|
@@ -8,8 +14,7 @@ import os
|
8
|
14
|
import re
|
9
|
15
|
import time
|
10
|
16
|
|
11
|
|
-VERSION = "0.1.0"
|
12
|
|
-STARTTIME = time.time()
|
|
17
|
+VERSION = "0.2.0"
|
13
|
18
|
logfile = None
|
14
|
19
|
|
15
|
20
|
HELPTEXT = """
|
|
@@ -27,16 +32,15 @@ F12: Quit the mqtt sniffer
|
27
|
32
|
"""
|
28
|
33
|
|
29
|
34
|
|
30
|
|
-def rx_mqtt(mc, userdata, message):
|
31
|
|
- global my_bb
|
|
35
|
+def msg_print_log(message, topic_regex, log2file):
|
32
|
36
|
global logfile
|
|
37
|
+
|
33
|
38
|
try:
|
34
|
|
- match = len(re.findall(my_bb.get_entry('msg_re'), message.topic)) > 0
|
|
39
|
+ match = len(re.findall(topic_regex, message.topic)) > 0
|
35
|
40
|
except re.error:
|
36
|
|
- print('No valid regular expression (%s). No filter active.' % my_bb.get_entry('msg_re'))
|
|
41
|
+ print('No valid regular expression (%s). No filter active.' % topic_regex)
|
37
|
42
|
match = True
|
38
|
43
|
|
39
|
|
- ts = time.time()-STARTTIME
|
40
|
44
|
data = None
|
41
|
45
|
try:
|
42
|
46
|
data = json.loads(message.payload)
|
|
@@ -48,13 +52,24 @@ def rx_mqtt(mc, userdata, message):
|
48
|
52
|
data = message.payload
|
49
|
53
|
|
50
|
54
|
if match:
|
51
|
|
- print("%9.04f::%75s::%s" % (ts, message.topic, data))
|
52
|
|
- if my_bb.get_entry('log2file'):
|
|
55
|
+ print("%25s::%75s::%s" % (time.asctime(), message.topic, data))
|
|
56
|
+ if log2file:
|
53
|
57
|
if logfile is None:
|
54
|
58
|
logfile = open('mqtt_sniffer.log', 'w')
|
55
|
|
- logfile.write("%9.04f::%s::%s\n" % (ts, message.topic, data))
|
|
59
|
+ logfile.write("%25s::%s::%s\n" % (time.asctime(), message.topic, data))
|
56
|
60
|
logfile.flush()
|
57
|
|
- bb.redraw()
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+def rx_mqtt(mc, userdata, message):
|
|
65
|
+ global my_bb
|
|
66
|
+ global args
|
|
67
|
+
|
|
68
|
+ if BottomBar is not None and bb is not None:
|
|
69
|
+ msg_print_log(message, my_bb.get_entry('msg_re'), my_bb.get_entry('log2file'))
|
|
70
|
+ bb.redraw()
|
|
71
|
+ else:
|
|
72
|
+ msg_print_log(message, args.topicfilter, args.logtofile)
|
58
|
73
|
|
59
|
74
|
|
60
|
75
|
if __name__ == "__main__":
|
|
@@ -76,12 +91,17 @@ if __name__ == "__main__":
|
76
|
91
|
args.username = None
|
77
|
92
|
password = None
|
78
|
93
|
|
79
|
|
- my_bb = BottomBar(VERSION, label='MQTT-Sniffer')
|
80
|
|
- my_bb.add_entry('help', 1, my_bb.FUNC_INFO, label='[F1] Help', infotext=HELPTEXT)
|
81
|
|
- my_bb.add_entry('msg_re', 2, my_bb.FUNC_TEXT, label='[F2] Filter', default=args.topicfilter)
|
82
|
|
- my_bb.add_entry('quit', 12, my_bb.FUNC_QUIT, "Quit", label='[F12]', right=True)
|
83
|
|
- my_bb.add_entry('log2file', 9, my_bb.FUNC_BOOL, label='[F9] Log2File', default=args.logtofile, right=True)
|
|
94
|
+ if BottomBar != None:
|
|
95
|
+ my_bb = BottomBar(VERSION, label='MQTT-Sniffer')
|
|
96
|
+ my_bb.add_entry('help', 1, my_bb.FUNC_INFO, label='[F1] Help', infotext=HELPTEXT)
|
|
97
|
+ my_bb.add_entry('msg_re', 2, my_bb.FUNC_TEXT, label='[F2] Filter', default=args.topicfilter)
|
|
98
|
+ my_bb.add_entry('quit', 12, my_bb.FUNC_QUIT, "Quit", label='[F12]', right=True)
|
|
99
|
+ my_bb.add_entry('log2file', 9, my_bb.FUNC_BOOL, label='[F9] Log2File', default=args.logtofile, right=True)
|
84
|
100
|
|
85
|
101
|
mc = mqtt.mqtt_client("mqtt_sniffer", args.hostname, port=args.port, username=args.username, password=password)
|
86
|
102
|
mc.add_callback("#", rx_mqtt)
|
87
|
|
- my_bb.run()
|
|
103
|
+ if BottomBar != None:
|
|
104
|
+ my_bb.run()
|
|
105
|
+ else:
|
|
106
|
+ while True:
|
|
107
|
+ time.sleep(1)
|