README.md

Bash-MQTT-Command-Linux

Bash script for executing commands remotely using MQTT

This script connects to an MQTT broker and subscribes to a topic called device/command. It waits for incoming messages, which are expected to be shell commands to be executed on the current machine. The script executes the received command and sends the result back to the MQTT broker on a topic called openwrt/response.

Requirements

  1. Mosquitto MQTT Client: The script uses mosquitto_pub and mosquitto_sub to interact with the MQTT broker. Ensure that these tools are installed and accessible in your system’s PATH.

  2. MQTT Broker: You need a running MQTT broker that the script can connect to. The broker details (host, port, username, password) should be correctly set in the script.

  3. Topics: The script subscribes to a command topic (device/command) and publishes responses to a response topic (device/response). Ensure these topics exist on your MQTT broker.

  4. Client ID: The script uses a unique client ID (device) to connect to the MQTT broker. Make sure this ID is unique within your broker.

  5. Credentials: The script requires a username and password to authenticate with the MQTT broker. Ensure these are correctly set in the script.

Usage

  1. Set the MQTT broker details, topics, client ID, and credentials in the script.
broker="your.mqtt.broker"
port="your-mqtt-broker-port"
topic="device/command"
response_topic="device/response"
client_id="device"
username="your-mqtt-username"
password="your-mqtt-password"
  1. Run the script. It will start listening for commands on the command topic.

  2. To send a command, publish a message to the command topic with the command as the payload.

  3. The script will execute the command and send the output back to the response topic.

  4. The script will continue to listen for commands and execute them in a loop.

Notes

  • The script uses eval to execute the received command. Be cautious with this as it can pose a security risk if you’re executing arbitrary commands.
  • The script uses a loop to continuously listen for commands. If you want to stop the script, you can do so by sending an interrupt signal (e.g., Ctrl+C).
  • The script does not handle errors or exceptions. You may want to add error handling to make the script more robust.

License

This script is provided under the MIT License. You are free to use, modify, and distribute the script as per the terms of this license.

Описание

Bash-скрипт для удаленного выполнения команд с помощью MQTT

Конвейеры
0 успешных
0 с ошибкой