瀏覽代碼

Initial mqtt-sniffer

master
Dirk Alders 1 年之前
父節點
當前提交
8448db149b
共有 4 個檔案被更改,包括 81 行新增0 行删除
  1. 6
    0
      .gitmodules
  2. 1
    0
      console_bottombar
  3. 1
    0
      mqtt
  4. 73
    0
      mqtt-sniffer.py

+ 6
- 0
.gitmodules 查看文件

@@ -0,0 +1,6 @@
1
+[submodule "mqtt"]
2
+	path = mqtt
3
+	url = https://git.mount-mockery.de/pylib/mqtt.git
4
+[submodule "console_bottombar"]
5
+	path = console_bottombar
6
+	url = https://git.mount-mockery.de/pylib/console_bottombar.git

+ 1
- 0
console_bottombar

@@ -0,0 +1 @@
1
+Subproject commit ff0434370a4229019a2cefcb01bb159325ff96b2

+ 1
- 0
mqtt

@@ -0,0 +1 @@
1
+Subproject commit 1adfb0626e7777c6d29be65d4ad4ce2d57541301

+ 73
- 0
mqtt-sniffer.py 查看文件

@@ -0,0 +1,73 @@
1
+import argparse
2
+from console_bottombar import BottomBar
3
+import json
4
+import mqtt
5
+import os
6
+import re
7
+import time
8
+
9
+VERSION = "0.1.0"
10
+STARTTIME = time.time()
11
+# TODO: Implement default values for bottombar_entries
12
+# TODO: Implement the Filter functionality
13
+
14
+HELPTEXT = """
15
+F1: Get this help message
16
+F2: Set a filter (regular expression) for the topic of a message
17
+    Examples:
18
+    * "/gfw/*/videv" Get everything with "/gfw/" before "/videv" 
19
+F9: Start / Stop logging to mqtt-sniffer.log
20
+F12: Quit the mqtt sniffer
21
+"""
22
+
23
+def rx_mqtt(mc, userdata, message):
24
+    global my_bb
25
+    global logfile
26
+    try:
27
+        match = len(re.findall(my_bb.get_entry('msg_re'), message.topic)) > 0
28
+    except re.error:
29
+        print('No valid regular expression (%s). No filter active.' % my_bb.get_entry('msg_re'))
30
+        match = True
31
+
32
+    ts = time.time()-STARTTIME
33
+    data = None
34
+    try:
35
+        data = json.loads(message.payload)
36
+    except:
37
+        pass
38
+    try:
39
+        data = message.payload.decode("utf-8")
40
+    except:
41
+        data = message.payload
42
+
43
+    if match:
44
+        print("%9.04f::%30s::%s" % (ts, message.topic, data))
45
+        if my_bb.get_entry('log2file'):
46
+            logfile.write("%9.04f::%30s::%s\n" % (ts, message.topic, data))
47
+            logfile.flush()
48
+
49
+if __name__ == "__main__":
50
+    parser = argparse.ArgumentParser(description='This is a mqtt sniffer.')
51
+    parser.add_argument('-f', dest='hostname', default='localhost', help='Hostname of the mqtt server')
52
+    parser.add_argument('-p', dest='port', default=1883, help='Port of the mqtt server')
53
+    parser.add_argument('-n', dest='no_credentials', action='store_true', help='Avoid asking for credentials')
54
+    parser.add_argument('-u', dest='username', default=None, help='Set username for mqtt server')
55
+
56
+    args = parser.parse_args()
57
+
58
+    if not args.no_credentials:
59
+        if args.username == None:
60
+            args.username = input("Username: ")
61
+        password = ""
62
+    else:
63
+        password = None
64
+
65
+    my_bb = BottomBar(VERSION, label='MQTT-Sniffer')
66
+    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')
68
+    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)
70
+    with open('sniffer.log', 'w') as logfile:
71
+        mc = mqtt.mqtt_client("sniffer", args.hostname, port=args.port, username=args.username, password=password)
72
+        mc.add_callback("#", rx_mqtt)
73
+        my_bb.run()

Loading…
取消
儲存