Created
June 21, 2024 14:32
-
-
Save EleotleCram/4a8d13802dca444d0c50e880f791159f to your computer and use it in GitHub Desktop.
Manage Tasmota UFS from bash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Function to show help message | |
show_help() { | |
echo "Usage: $0 --hostname TASMOTA_DEVICE [OPTIONS] CMD [ARGS]" | |
echo "" | |
echo "Commands:" | |
echo " PUT FILE DEST_PATH Upload a file to the specified destination path." | |
echo " GET FILE_PATH [-o OUTPUT_FILE] Download a file from the specified path." | |
echo " GET FILE_PATH TARGET_DIR Download a file from the specified path to the target directory." | |
echo " DEL FILE_PATH Delete a file at the specified path." | |
echo "" | |
echo "Options:" | |
echo " --hostname TASMOTA_DEVICE Specify the Tasmota device hostname." | |
echo " -q, --quiet Enable quiet mode, suppressing success or failure messages." | |
echo " --help Show this help message and exit." | |
} | |
# Default values | |
QUIET=false | |
# Parse global options | |
while [[ $# -gt 0 ]]; do | |
case $1 in | |
--hostname) | |
TASMOTA_HOSTNAME="$2" | |
shift 2 | |
;; | |
-q|--quiet) | |
QUIET=true | |
shift | |
;; | |
--help) | |
show_help | |
exit 0 | |
;; | |
--*) | |
echo "Unknown option: $1" | |
show_help | |
exit 1 | |
;; | |
*) | |
break | |
;; | |
esac | |
done | |
# Check if no command was passed | |
if [ $# -eq 0 ]; then | |
show_help | |
exit 1 | |
fi | |
# Ensure hostname is specified | |
if [ -z "$TASMOTA_HOSTNAME" ]; then | |
echo "Error: --hostname option is required." | |
show_help | |
exit 1 | |
fi | |
# Command line arguments | |
CMD=$1 | |
shift | |
# Base URL | |
BASE_URL="http://${TASMOTA_HOSTNAME}" | |
# Function to upload a file | |
put_file() { | |
local FILE=$1 | |
local DEST_PATH=$2 | |
# Unfortunately the webserver upload target is stateful, so we first need to GET the DEST_PATH before we can upload to that path | |
if curl -o /dev/null -sS "${BASE_URL}/ufsd?download=${DEST_PATH}"; then | |
if curl -o /dev/null -sS --form "ufsu=@${FILE}" "${BASE_URL}/ufsu"; then | |
$QUIET || echo "File uploaded successfully." | |
return 0 | |
else | |
$QUIET || echo "File upload failed." | |
return 1 | |
fi | |
else | |
$QUIET || echo "File upload failed." | |
return 1 | |
fi | |
} | |
# Function to download a file | |
get_file() { | |
local FILE_PATH=$1 | |
local OUTPUT_FILE="${2:-$(basename "${FILE_PATH}")}" | |
if curl -o "${OUTPUT_FILE}" -sS "${BASE_URL}/ufsd?download=${FILE_PATH}"; then | |
$QUIET || echo "File downloaded successfully." | |
return 0 | |
else | |
$QUIET || echo "File download failed." | |
return 1 | |
fi | |
} | |
# Function to download a file to a target directory | |
get_file_to_dir() { | |
local FILE_PATH=$1 | |
local TARGET_DIR=$2 | |
local OUTPUT_FILE="${TARGET_DIR}/$(basename "${FILE_PATH}")" | |
if curl -o "${OUTPUT_FILE}" -sS "${BASE_URL}/ufsd?download=${FILE_PATH}"; then | |
$QUIET || echo "File downloaded successfully to ${TARGET_DIR}." | |
return 0 | |
else | |
$QUIET || echo "File download to ${TARGET_DIR} failed." | |
return 1 | |
fi | |
} | |
# Function to delete a file | |
del_file() { | |
local FILE_PATH=$1 | |
if curl -o /dev/null -s "${BASE_URL}/ufsd?delete=${FILE_PATH}"; then | |
$QUIET || echo "File deleted successfully." | |
return 0 | |
else | |
$QUIET || echo "File deletion failed." | |
return 1 | |
fi | |
} | |
# Execute command | |
case $CMD in | |
PUT) | |
if [ $# -ne 2 ]; then | |
echo "Error: PUT requires 2 arguments (FILE DEST_PATH)" | |
show_help | |
exit 1 | |
fi | |
put_file "$1" "$2" | |
;; | |
GET) | |
if [ $# -eq 1 ]; then | |
get_file "$1" | |
elif [ "$2" == "-o" ] && [ $# -eq 3 ]; then | |
get_file "$1" "$3" | |
elif [ $# -eq 2 ] && [ -d "$2" ]; then | |
get_file_to_dir "$1" "$2" | |
else | |
echo "Error: Invalid arguments for GET command." | |
show_help | |
exit 1 | |
fi | |
;; | |
DEL) | |
if [ $# -ne 1 ]; then | |
echo "Error: DEL requires 1 argument (FILE_PATH)" | |
show_help | |
exit 1 | |
fi | |
del_file "$1" | |
;; | |
*) | |
echo "Error: Unknown command '$CMD'" | |
show_help | |
exit 1 | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment