A mqtt sniffer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mqtt_sniffer.py 3.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import argparse
  2. from console_bottombar import BottomBar
  3. import getpass
  4. import json
  5. import mqtt
  6. import os
  7. import re
  8. import time
  9. VERSION = "0.1.0"
  10. STARTTIME = time.time()
  11. logfile = None
  12. HELPTEXT = """
  13. F1: Get this help message
  14. F2: Set a filter (regular expression) for the topic of a message
  15. Examples:
  16. * "/gfw/.*/main_light/" to get everything with "/gfw/" before "/main_light/"
  17. * "^zigbee.*(?>!logging)$" to get everything starting with "zigbee" and not ending with "logging"
  18. * "^(?!shellies).*/dirk/.*temperature$" to get everything not starting with "shellies" followed by "/dirk/" and ends with "temperature"
  19. F9: Start / Stop logging to mqtt-sniffer.log
  20. F12: Quit the mqtt sniffer
  21. 'c': Clear screen
  22. 'q': Quit
  23. """
  24. def rx_mqtt(mc, userdata, message):
  25. global my_bb
  26. global logfile
  27. try:
  28. match = len(re.findall(my_bb.get_entry('msg_re'), message.topic)) > 0
  29. except re.error:
  30. print('No valid regular expression (%s). No filter active.' % my_bb.get_entry('msg_re'))
  31. match = True
  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. if match:
  43. print("%9.04f::%75s::%s" % (ts, message.topic, data))
  44. if my_bb.get_entry('log2file'):
  45. if logfile is None:
  46. logfile = open('mqtt_sniffer.log', 'w')
  47. logfile.write("%9.04f::%s::%s\n" % (ts, message.topic, data))
  48. logfile.flush()
  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. parser.add_argument('-t', dest='topicfilter', default="", help='Set topic filter')
  56. parser.add_argument('-l', dest='logtofile', action='store_true', help='Enable logging to file')
  57. args = parser.parse_args()
  58. if not args.no_credentials:
  59. if args.username == None:
  60. args.username = input("Username: ")
  61. password = getpass.getpass(prompt='Password: ', stream=None)
  62. else:
  63. args.username = None
  64. password = None
  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', default=args.topicfilter)
  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', default=args.logtofile, right=True)
  70. mc = mqtt.mqtt_client("mqtt_sniffer", args.hostname, port=args.port, username=args.username, password=password)
  71. mc.add_callback("#", rx_mqtt)
  72. my_bb.run()