Skip to content
121 changes: 121 additions & 0 deletions extras/scripts/transmission-port-update.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
#!/bin/sh
Comment thread
juanlufont marked this conversation as resolved.

# Description: This script updates the peer port for the Transmission torrent
# client using its RPC API.
# Author: Juan Luis Font

# default values
DEFAULT_URL="http://localhost:9091/transmission/rpc"
WGET_OPTS="--quiet"
Comment thread
qdm12 marked this conversation as resolved.
Outdated

usage() {
echo "Usage: $0 [OPTIONS]"
echo
echo "Options:"
echo " -h, --help Show this help message and exit."
echo " -u, --user USER Specify the Transmission RPC user name."
echo " (Omit if not required)"
echo " -p, --pass PASS Specify the Transmission RPC password."
echo " (Omit if not required)"
echo " -P, --port PORT Specify the Transmission Peer Port."
echo " REQUIRED"
Comment thread
qdm12 marked this conversation as resolved.
echo " -U, --url URL Specify the Transmission RPC URL."
echo " DEFAULT: ${DEFAULT_URL}"
echo "Example:"
echo " $0 -u admin -p **** 40409"
exit 1
}

while [ $# -gt 0 ]; do
case "$1" in
-h | --help)
usage
;;
-u | --user)
USERNAME="$2"
_USECRED=true
shift 2
;;
-p | --pass)
PASSWORD="$2"
_USECRED=true
shift 2
;;
-P | --port)
PORT="$2"
shift 2
;;
-U | --url)
RPC_URL="$2"
shift 2
;;
*)
echo "Unknown option: $1"
usage
;;
esac
done

if [ -z "${PORT}" ]; then
echo "ERROR: No PORT provided!"
exit 1
fi

if [ "${_USECRED}" ]; then
# make sure username AND password were provided
if [ -z "${USERNAME}" ] || [ -z "${PASSWORD}" ]; then
echo "ERROR: Username or password not provided."
exit 1
Comment thread
juanlufont marked this conversation as resolved.
else
Comment thread
juanlufont marked this conversation as resolved.
Outdated
# basic auth options, --auth-no-challenge avoids 409 Conflict
WGET_OPTS="
${WGET_OPTS}
Comment thread
juanlufont marked this conversation as resolved.
Outdated
--auth-no-challenge
--user=${USERNAME}
--password=${PASSWORD}
"
fi
fi

if [ -z "${RPC_URL+x}" ]; then
RPC_URL="${DEFAULT_URL}"
fi

# get the X-Transmission-Session-Id
SESSION_ID=$(
wget \
${WGET_OPTS} \
--server-response \
"$RPC_URL" 2>&1 |
grep 'X-Transmission-Session-Id:' |
awk '{print $2}'
)
Comment on lines +92 to +102

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consider wrapping this in a loop, which fails after some retries:

i=1
while [ "$i" -le $MAX_RETRIES ]; do
    # get the X-Transmission-Session-Id
    # shellcheck disable=SC2086
    SESSION_ID=$(
        wget \
            --quiet \
            ${WGET_OPTS} \
            --server-response \
            "$RPC_URL" 2>&1 |
            grep 'X-Transmission-Session-Id:' |
            awk '{print $2}'
    )
    
    if [ -n "$SESSION_ID" ]; then
        echo "Transmission available. SESSION_ID: $SESSION_ID"
        break
    else
        echo "Attempt $i of $MAX_RETRIES failed, waiting for next attempt..."
        sleep 5
    fi

    i=$(( i + 1 ))
done


# generate payload string
PAYLOAD=$(printf '{
"method": "session-set",
"arguments": {
"peer-port": %s
}
}' "$PORT")

# update peer host via API
RES=$(
wget \
${WGET_OPTS} \
--header="Content-Type: application/json" \
--header="X-Transmission-Session-Id: $SESSION_ID" \
--post-data="${PAYLOAD}" \
"$RPC_URL" \
-O -
)

# check string returned by wget
SUCCESS='{"arguments":{},"result":"success"}'
if [ "$RES" = "$SUCCESS" ]; then
echo "Success! Peer port updated to ${PORT}"
exit 0
else
Comment thread
juanlufont marked this conversation as resolved.
Outdated
echo "ERROR: Could not update peer port"
exit 1
fi