A mqtt sniffer
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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