#!/bin/sh set -e usage() { echo "usage: $0 [-h|--help] [-t ]" >&2 exit } die() { echo "error: $1" >&2 exit 1 } check_command() { if ! command -v "$1" >/dev/null; then die "$1 is not installed. Please install $1 to proceed." fi } parse_yaml() { if ! yaml_output=$(yq -r "$1" "$2" 2>/dev/null); then die "YAML file is malformed or does not exist." fi echo "$yaml_output" } for c in yq mosquitto_sub tput; do check_command $c; done [ -z "$1" ] && usage bold=$(tput bold) rst=$(tput sgr0) topic= while [ $# -gt 0 ]; do case "$1" in -h|--help) usage exit 0 ;; -t|--topic) topic="$2" shift ;; *) ;; esac shift done [ -z "$topic" ] && die "Topic not provided. Use -t or --topic to specify the topic." remote_host="mqtt.example.org" remote_port=8883 username="admin" password="password" mydir=$(dirname "$0") cafile="$mydir/mqtt_ca.crt" topic_regex="${topic%#}" topic_regex="${topic_regex//\//\\/}" echo mosquitto_sub -d -h "$remote_host" -p "$remote_port" --cafile "$cafile" -t "$topic" -u "$username" -P "$password" -v mosquitto_sub -d -h "$remote_host" -p "$remote_port" --cafile "$cafile" -t "$topic" -u "$username" -P "$password" -v | while IFS= read -r line; do binary_data="$(echo "$line" | sed "s/^${topic_regex}[^ ]* //")" echo -n "${bold}$(echo "$line" | awk '{print $1}')${rst}" echo -n " " echo -n "$binary_data" | xxd -p | tr -d '\n' | sed 's/../& /g' echo # echo done