Advertisement
Guest User

Untitled

a guest
Apr 18th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. #!/usr/bin/env lua
  2.  
  3. local mqttclient = require("luamqttc/client")
  4. local argparse = require "argparse"
  5. local parser = argparse() {
  6. name = "mqtt_dump",
  7. description = "A script to save mqtt subscription values.",
  8. }
  9. parser:option "-h" "--host"
  10. :argname "<server hostname>"
  11. parser:option("-o --output", "<output file>", "mqtt_dump.log")
  12. parser:option("-h --host", "<server hostname>", "localhost")
  13. parser:option("-p --port", "<port number>", "1883")
  14. parser:option("-u --user", "<user>", "")
  15. parser:option("-p --pass", "<password>", "")
  16. parser:option("-t --topic", "<topic>", "#")
  17.  
  18. local args = parser:parse()
  19. local host = args.host
  20. local port = args.port
  21. local user = args.user
  22. local pass = args.pass
  23. local dump_topic = args.topic
  24. local timeout = 1
  25.  
  26. file = io.open(args.output, "a")
  27. local aclient = mqttclient.new("mqtt_dump",{username = user, password = pass, keep_alive = 180})
  28.  
  29. local callback = function(topic, data, packet_id, dup, qos, retained)
  30. print("DUMP: ", os.time(), topic, data, qos)
  31. file:write(os.time(),";",topic,";", data,"\n")
  32. end
  33.  
  34. aclient:connect(host, port, {timeout = timeout})
  35. aclient:subscribe(dump_topic, 2, callback)
  36. aclient:message_loop()
  37. aclient:disconnect()
  38. file:close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement