-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmqtt_command.sh
59 lines (52 loc) · 1.63 KB
/
mqtt_command.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/bin/bash
# Check if the config file exists
if [ ! -f "./config.ini" ]; then
echo "Config file not found."
exit 1
fi
# Source the configuration file
. ./config.ini
# Function for sending messages
send_message() {
local lines=("$@")
mosquitto_pub -h "$broker" -p "$port" -i "$client_id" -t "$response_topic" -u "$username" -P "$password" -m "$(printf "%s\n" "${lines[@]}")" >> "$LOG_FILE" 2>&1
}
# Function for processing received messages
on_message() {
message=$1
echo "$(date): Processing message: $message" >> "$LOG_FILE"
if [ "$USE_WHITELIST" = "yes" ]; then
# Use command whitelist for security
case "$message" in
"status")
result="System is running"
;;
"version")
result=$(cat /etc/os-release)
;;
*)
result="Unknown command"
;;
esac
else
# WARNING: Executing received command directly. This is insecure.
# Log the command for auditing purposes
echo "$(date): Executing command: $message" >> "$LOG_FILE"
# Execute the command and capture output
result=$(eval "$message" 2>&1)
if [ $? -ne 0 ]; then
result="Command failed: $result"
fi
fi
echo "$(date): Sending response: $result" >> "$LOG_FILE"
send_message "$result"
}
# Main loop to receive and process messages
while true
do
message=$(mosquitto_sub -h "$broker" -p "$port" -i "$client_id" -t "$topic" -u "$username" -P "$password" -C 1)
if [ -n "${message// }" ]; then
on_message "$message"
fi
sleep 1
done