A mqtt sniffer
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

mqtt_sniffer.py 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import bottombar as bb
  2. import argparse
  3. from console_bottombar import BottomBar
  4. import getpass
  5. import json
  6. import mqtt
  7. import os
  8. import re
  9. import time
  10. VERSION = "0.1.0"
  11. STARTTIME = time.time()
  12. logfile = None
  13. HELPTEXT = """
  14. F1: Get this help message
  15. F2: Set a filter (regular expression) for the topic of a message
  16. Examples:
  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
  21. F12: Quit the mqtt sniffer
  22. 'c': Clear screen
  23. 'q': Quit
  24. """
  25. def rx_mqtt(mc, userdata, message):
  26. global my_bb
  27. global logfile
  28. try:
  29. match = len(re.findall(my_bb.get_entry('msg_re'), message.topic)) > 0
  30. except re.error:
  31. print('No valid regular expression (%s). No filter active.' % my_bb.get_entry('msg_re'))
  32. match = True
  33. ts = time.time()-STARTTIME
  34. data = None
  35. try:
  36. data = json.loads(message.payload)
  37. except:
  38. pass
  39. try:
  40. data = message.payload.decode("utf-8")
  41. except:
  42. data = message.payload
  43. if match:
  44. print("%9.04f::%75s::%s" % (ts, message.topic, data))
  45. if my_bb.get_entry('log2file'):
  46. if logfile is None:
  47. logfile = open('mqtt_sniffer.log', 'w')
  48. logfile.write("%9.04f::%s::%s\n" % (ts, message.topic, data))
  49. logfile.flush()
  50. bb.redraw()
  51. if __name__ == "__main__":
  52. parser = argparse.ArgumentParser(description='This is a mqtt sniffer.')
  53. parser.add_argument('-f', dest='hostname', default='localhost', help='Hostname of the mqtt server')
  54. parser.add_argument('-p', dest='port', default=1883, help='Port of the mqtt server')
  55. parser.add_argument('-n', dest='no_credentials', action='store_true', help='Avoid asking for credentials')
  56. parser.add_argument('-u', dest='username', default=None, help='Set username for mqtt server')
  57. parser.add_argument('-t', dest='topicfilter', default="", help='Set topic filter')
  58. parser.add_argument('-l', dest='logtofile', action='store_true', help='Enable logging to file')
  59. args = parser.parse_args()
  60. if not args.no_credentials:
  61. if args.username == None:
  62. args.username = input("Username: ")
  63. password = getpass.getpass(prompt='Password: ', stream=None)
  64. else:
  65. args.username = None
  66. password = None
  67. my_bb = BottomBar(VERSION, label='MQTT-Sniffer')
  68. my_bb.add_entry('help', 1, my_bb.FUNC_INFO, label='[F1] Help', infotext=HELPTEXT)
  69. my_bb.add_entry('msg_re', 2, my_bb.FUNC_TEXT, label='[F2] Filter', default=args.topicfilter)
  70. my_bb.add_entry('quit', 12, my_bb.FUNC_QUIT, "Quit", label='[F12]', right=True)
  71. my_bb.add_entry('log2file', 9, my_bb.FUNC_BOOL, label='[F9] Log2File', default=args.logtofile, right=True)
  72. mc = mqtt.mqtt_client("mqtt_sniffer", args.hostname, port=args.port, username=args.username, password=password)
  73. mc.add_callback("#", rx_mqtt)
  74. my_bb.run()