Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # CONFIGURATION
- GLUETUN_PORT_FILE="/container_path/gluetun/forwarded_port"
- QBIT_HOST="http://localhost:8080"
- QBIT_USER="[redacted]"
- QBIT_PASS="[redacted]"
- QBIT_CONTAINER_NAME="qbittorrent"
- COOKIE_JAR="/tmp/qbit_cookies.txt"
- # Check port file exists
- if [[ ! -f "$GLUETUN_PORT_FILE" ]]; then
- echo "[!] Port file not found: $GLUETUN_PORT_FILE"
- exit 1
- fi
- # Read forwarded port from Gluetun
- FORWARDED_PORT=$(cat "$GLUETUN_PORT_FILE" | tr -cd '[:digit:]')
- if [[ -z "$FORWARDED_PORT" ]]; then
- echo "[!] Forwarded port is empty or invalid."
- exit 1
- fi
- # Login to qBittorrent API
- curl -s --cookie-jar "$COOKIE_JAR" \
- -d "username=$QBIT_USER&password=$QBIT_PASS" \
- "$QBIT_HOST/api/v2/auth/login" > /dev/null
- # Get current qBittorrent port
- CURRENT_PORT=$(curl -s --cookie "$COOKIE_JAR" \
- "$QBIT_HOST/api/v2/app/preferences" | jq .listen_port)
- # Fallback if port couldn't be read
- if ! [[ "$CURRENT_PORT" =~ ^[0-9]+$ ]]; then
- echo "[!] Failed to read current qBittorrent port. Got: $CURRENT_PORT"
- exit 1
- fi
- # Compare and update if needed
- if [[ "$CURRENT_PORT" -ne "$FORWARDED_PORT" ]]; then
- echo "[*] Updating qBittorrent port to $FORWARDED_PORT"
- curl -s --cookie "$COOKIE_JAR" \
- -d "json={\"listen_port\":$FORWARDED_PORT}" \
- "$QBIT_HOST/api/v2/app/setPreferences" > /dev/null
- # Restart qBittorrent container to apply changes (if needed)
- sudo /usr/bin/docker restart "$QBIT_CONTAINER_NAME" >/dev/null 2>&1
- else
- # Optional: silent success
- :
- fi
- # Cleanup
- rm -f "$COOKIE_JAR"
Add Comment
Please, Sign In to add comment