Skip to content

Instantly share code, notes, and snippets.

@AgustinParmisano
Last active January 31, 2018 20:38
Show Gist options
  • Select an option

  • Save AgustinParmisano/22bf357238e6f15896d0d12b58bd7d63 to your computer and use it in GitHub Desktop.

Select an option

Save AgustinParmisano/22bf357238e6f15896d0d12b58bd7d63 to your computer and use it in GitHub Desktop.
Bash auto mqtt pub script with cofigurable broker, msg and topic
#!/bin/bash
read -p "[!] Enter Broker IP or URL (f.e. broker.hivemq.com): " broker
echo "[!] Testing broker $broker connection with ping . . . "
echo "[!] Please wait . . ."
brokeron=$(ping -c 4 $broker)
if [ $? -gt 0 ]; then
echo "[-] Broker $broker cant respond to ping, try another broker"
echo "[-] Exiting with error 123."
exit 123
fi
echo "[!] Broker $broker ping responded!"
read -p "[!] Enter topic (f.e. test): " topic
read -p "[!] Enter message to send (f.e. helloworld): " msg
echo "[!] Sending mqtt packets to $broker with topic $topic and message $msg (ctrl + c to stop)"
while true; do
echo ""
msgans=$(mosquitto_pub -h $broker -t $topic -m $msg)
stnderror=$?
echo $msgans
if [ $stnderror -eq 7 ]; then
echo "[-] MQTT Msg to broker $broker respond with error 7: Connection Lost."
echo "[-] Exiting with error 7."
exit 5
fi
echo "[+] Message $msg correctly sent to broker $broker with topic $topic."
sleep 5
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment