A mqtt sniffer
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

mqtt_sniffer.py 2.5KB

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