|
@@ -9,17 +9,22 @@ import time
|
9
|
9
|
|
10
|
10
|
VERSION = "0.1.0"
|
11
|
11
|
STARTTIME = time.time()
|
12
|
|
-# TODO: Implement default values for bottombar_entries
|
13
|
12
|
|
14
|
13
|
HELPTEXT = """
|
15
|
14
|
F1: Get this help message
|
16
|
15
|
F2: Set a filter (regular expression) for the topic of a message
|
17
|
16
|
Examples:
|
18
|
|
- * "/gfw/.*/videv" Get everything with "/gfw/" before "/videv"
|
19
|
|
-F9: Start / Stop logging to mqtt_sniffer.log
|
|
17
|
+ * "/gfw/.*/main_light/" to get everything with "/gfw/" before "/main_light/"
|
|
18
|
+ * "^zigbee.*(?>!logging)$" to get everything starting with "zigbee" and not ending with "logging"
|
|
19
|
+ * "^(?!shellies).*/dirk/.*temperature$" to get everything not starting with "shellies" followed by "/dirk/" and ends with "temperature"
|
|
20
|
+F9: Start / Stop logging to mqtt-sniffer.log
|
20
|
21
|
F12: Quit the mqtt sniffer
|
|
22
|
+
|
|
23
|
+'c': Clear screen
|
|
24
|
+'q': Quit
|
21
|
25
|
"""
|
22
|
26
|
|
|
27
|
+
|
23
|
28
|
def rx_mqtt(mc, userdata, message):
|
24
|
29
|
global my_bb
|
25
|
30
|
global logfile
|
|
@@ -46,27 +51,31 @@ def rx_mqtt(mc, userdata, message):
|
46
|
51
|
logfile.write("%9.04f::%s::%s\n" % (ts, message.topic, data))
|
47
|
52
|
logfile.flush()
|
48
|
53
|
|
|
54
|
+
|
49
|
55
|
if __name__ == "__main__":
|
50
|
56
|
parser = argparse.ArgumentParser(description='This is a mqtt sniffer.')
|
51
|
57
|
parser.add_argument('-f', dest='hostname', default='localhost', help='Hostname of the mqtt server')
|
52
|
58
|
parser.add_argument('-p', dest='port', default=1883, help='Port of the mqtt server')
|
53
|
59
|
parser.add_argument('-n', dest='no_credentials', action='store_true', help='Avoid asking for credentials')
|
54
|
60
|
parser.add_argument('-u', dest='username', default=None, help='Set username for mqtt server')
|
|
61
|
+ parser.add_argument('-t', dest='topicfilter', default="", help='Set topic filter')
|
|
62
|
+ parser.add_argument('-l', dest='logtofile', action='store_true', help='Enable logging to file')
|
55
|
63
|
|
56
|
64
|
args = parser.parse_args()
|
57
|
65
|
|
58
|
66
|
if not args.no_credentials:
|
59
|
67
|
if args.username == None:
|
60
|
68
|
args.username = input("Username: ")
|
61
|
|
- password = getpass.getpass(prompt='Password: ', stream=None)
|
|
69
|
+ password = getpass.getpass(prompt='Password: ', stream=None)
|
62
|
70
|
else:
|
|
71
|
+ args.username = None
|
63
|
72
|
password = None
|
64
|
73
|
|
65
|
74
|
my_bb = BottomBar(VERSION, label='MQTT-Sniffer')
|
66
|
75
|
my_bb.add_entry('help', 1, my_bb.FUNC_INFO, label='[F1] Help', infotext=HELPTEXT)
|
67
|
|
- my_bb.add_entry('msg_re', 2, my_bb.FUNC_TEXT, label='[F2] Filter')
|
|
76
|
+ my_bb.add_entry('msg_re', 2, my_bb.FUNC_TEXT, label='[F2] Filter', default=args.topicfilter)
|
68
|
77
|
my_bb.add_entry('quit', 12, my_bb.FUNC_QUIT, "Quit", label='[F12]', right=True)
|
69
|
|
- my_bb.add_entry('log2file', 9, my_bb.FUNC_BOOL, label='[F9] Log2File', right=True)
|
|
78
|
+ my_bb.add_entry('log2file', 9, my_bb.FUNC_BOOL, label='[F9] Log2File', default=args.logtofile, right=True)
|
70
|
79
|
with open('mqtt_sniffer.log', 'w') as logfile:
|
71
|
80
|
mc = mqtt.mqtt_client("mqtt_sniffer", args.hostname, port=args.port, username=args.username, password=password)
|
72
|
81
|
mc.add_callback("#", rx_mqtt)
|