beepflix

update-qbittorrent-port.sh

Jul 14th, 2025 (edited)
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # CONFIGURATION
  4. GLUETUN_PORT_FILE="/container_path/gluetun/forwarded_port"
  5. QBIT_HOST="http://localhost:8080"
  6. QBIT_USER="[redacted]"
  7. QBIT_PASS="[redacted]"
  8. QBIT_CONTAINER_NAME="qbittorrent"
  9. COOKIE_JAR="/tmp/qbit_cookies.txt"
  10.  
  11. # Check port file exists
  12. if [[ ! -f "$GLUETUN_PORT_FILE" ]]; then
  13. echo "[!] Port file not found: $GLUETUN_PORT_FILE"
  14. exit 1
  15. fi
  16.  
  17. # Read forwarded port from Gluetun
  18. FORWARDED_PORT=$(cat "$GLUETUN_PORT_FILE" | tr -cd '[:digit:]')
  19. if [[ -z "$FORWARDED_PORT" ]]; then
  20. echo "[!] Forwarded port is empty or invalid."
  21. exit 1
  22. fi
  23.  
  24. # Login to qBittorrent API
  25. curl -s --cookie-jar "$COOKIE_JAR" \
  26. -d "username=$QBIT_USER&password=$QBIT_PASS" \
  27. "$QBIT_HOST/api/v2/auth/login" > /dev/null
  28.  
  29. # Get current qBittorrent port
  30. CURRENT_PORT=$(curl -s --cookie "$COOKIE_JAR" \
  31. "$QBIT_HOST/api/v2/app/preferences" | jq .listen_port)
  32.  
  33. # Fallback if port couldn't be read
  34. if ! [[ "$CURRENT_PORT" =~ ^[0-9]+$ ]]; then
  35. echo "[!] Failed to read current qBittorrent port. Got: $CURRENT_PORT"
  36. exit 1
  37. fi
  38.  
  39. # Compare and update if needed
  40. if [[ "$CURRENT_PORT" -ne "$FORWARDED_PORT" ]]; then
  41. echo "[*] Updating qBittorrent port to $FORWARDED_PORT"
  42.  
  43. curl -s --cookie "$COOKIE_JAR" \
  44. -d "json={\"listen_port\":$FORWARDED_PORT}" \
  45. "$QBIT_HOST/api/v2/app/setPreferences" > /dev/null
  46.  
  47. # Restart qBittorrent container to apply changes (if needed)
  48. sudo /usr/bin/docker restart "$QBIT_CONTAINER_NAME" >/dev/null 2>&1
  49. else
  50. # Optional: silent success
  51. :
  52. fi
  53.  
  54. # Cleanup
  55. rm -f "$COOKIE_JAR"
  56.  
  57.  
Add Comment
Please, Sign In to add comment