Add support for multiple commands (says claudegpt)

This commit is contained in:
2025-10-06 10:07:40 +02:00
parent cc860cb554
commit 9ed55d2171

View File

@@ -19,7 +19,10 @@ else
SESSION="" SESSION=""
# Command to run (leave empty to use command line arguments) # Command to run (leave empty to use command line arguments)
# For multiple commands, use an array:
# COMMANDS=("ssh root@server" "sudo su" "password123")
COMMAND="" COMMAND=""
COMMANDS=()
# Whether to attach to session after running command (0 or 1) # Whether to attach to session after running command (0 or 1)
ATTACH_SESSION=0 ATTACH_SESSION=0
@@ -29,8 +32,8 @@ EOF
fi fi
# Validate required variables # Validate required variables
if [ -z "$SESSION" ] && [ -z "$COMMAND" ]; then if [ -z "$SESSION" ] && [ -z "$COMMAND" ] && [ ${#COMMANDS[@]} -eq 0 ]; then
echo "Error: Either SESSION or COMMAND must be set in $CONFIG_FILE" >&2 echo "Error: Either SESSION, COMMAND, or COMMANDS must be set in $CONFIG_FILE" >&2
exit 1 exit 1
fi fi
@@ -39,11 +42,14 @@ if [ $# -gt 0 ]; then
COMMAND="$*" COMMAND="$*"
echo "Using command line arguments: $COMMAND" echo "Using command line arguments: $COMMAND"
else else
if [ -z "$COMMAND" ]; then if [ -n "$COMMAND" ]; then
echo "Error: No command provided and COMMAND not set in config" >&2 echo "Using config command: $COMMAND"
elif [ ${#COMMANDS[@]} -gt 0 ]; then
echo "Using config commands: ${COMMANDS[*]}"
else
echo "Error: No command provided and no commands set in config" >&2
exit 1 exit 1
fi fi
echo "Using config command: $COMMAND"
fi fi
# Use session name from config, or first word of command if not set # Use session name from config, or first word of command if not set
@@ -59,11 +65,22 @@ else
echo "Session $SESSION exists, reusing..." echo "Session $SESSION exists, reusing..."
fi fi
# Send command to session # Send command(s) to session
if [ ${#COMMANDS[@]} -gt 0 ]; then
echo "Running multiple commands: ${COMMANDS[*]}"
for cmd in "${COMMANDS[@]}"; do
echo "Running: $cmd"
printf '%s\n' "$cmd" | tmux load-buffer -
tmux paste-buffer -t $SESSION
tmux send-keys -t $SESSION C-m
sleep 1 # Brief pause between commands
done
else
echo "Running command: $COMMAND" echo "Running command: $COMMAND"
printf '%s\n' "$COMMAND" | tmux load-buffer - printf '%s\n' "$COMMAND" | tmux load-buffer -
tmux paste-buffer -t $SESSION tmux paste-buffer -t $SESSION
tmux send-keys -t $SESSION C-m tmux send-keys -t $SESSION C-m
fi
if [ $ATTACH_SESSION -eq 1 ]; then if [ $ATTACH_SESSION -eq 1 ]; then
echo "Attaching to tmux session: $SESSION" echo "Attaching to tmux session: $SESSION"