Guest User

Untitled

a guest
May 8th, 2020
101
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2. # Connect to MQTT server and dump all topics to file
  3. host="" # Target MQTT server IP address
  4. topic="" # MQTT server topic
  5. topicoption="" # Value to set to MQTT topic
  6.  
  7. checkdb (){
  8. grep -q "$host" host-list
  9. if [ $? == 0 ]; then
  10. echo "Host Found in database"
  11. else
  12. echo "$host" >> host-list
  13. fi
  14. }
  15.  
  16. help (){
  17. echo "usage: $0 [command] [host]"
  18. echo -e "Commands:\n$0 dump [host]\n$0 info [host]\n$0 set [topic] [option] [host]\n$0 update\n$0 show [host]"
  19. }
  20. if [ ! -a host-list ]; then
  21. touch host-list
  22. fi
  23. case $1 in
  24. dump) # Get all topics from MQTT server
  25. host=$2
  26. echo "Dumping $host topics"
  27. mosquitto_sub -W 15 -q 1 -h $host -t \# -v > mqtt-$host
  28. checkdb
  29. echo "Done. Report saved to mqtt-$host"
  30. exit
  31. ;;
  32. info) # Get MQTT server info
  33. host=$2
  34. echo "Getting system info for $host"
  35. mosquitto_sub -W 15 -q 1 -h $host -t \$SYS/# -v
  36. checkdb
  37. echo "Done"
  38. exit
  39. ;;
  40. set) # Change a MQTT server topic option
  41. host=$4
  42. topic=$2
  43. topicoption=$3
  44. mosquitto_pub -W 15 -q 1 -h $host -t $topic -m $topicoption -v
  45. checkdb
  46. echo "Done"
  47. exit
  48. ;;
  49. update) # Update topic subscriptions
  50. echo "Updating host database"
  51. for host in $( cat host-list )
  52. do
  53. mosquitto_sub -W 15 -q 1 -h $host -t \# -v > mqtt-$host
  54. done
  55. exit
  56. ;;
  57. show) # Shows topics and settings for a host
  58. host=$2
  59. echo -e "Report for $2:\n"
  60. if [ -s mqtt-$host ]; then
  61. cat mqtt-host
  62. else
  63. echo "No report for $host"
  64. fi
  65. exit
  66. ;;
  67. *)
  68. help
  69. exit
  70. esac
RAW Paste Data