Guest User

Untitled

a guest
Aug 31st, 2025
33
0
176 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Config variables
  4. QB_WEBUI_URL="http://qbittorrent:port"
  5. QB_API_USER="redacted"
  6. QB_API_PASS="redacted"
  7. LOGFILE="/directory/to/save/port_update.log"
  8.  
  9. # Keep only the last 150 lines
  10. tail -n 150 "$LOGFILE" > "${LOGFILE}.tmp" 2>/dev/null && mv "${LOGFILE}.tmp" "$LOGFILE"
  11.  
  12. # Get the forwarded port from PIA
  13. FORWARDED_PORT=$(piactl get portforward)
  14.  
  15. # Check if the forwarded port is valid
  16. if [[ "$FORWARDED_PORT" == "Inactive" || "$FORWARDED_PORT" == "Unavailable" ]]; then
  17. echo "$(date): PIA port forwarding is not available." >> "$LOGFILE"
  18. exit 1
  19. fi
  20.  
  21. # Update qBittorrent's listening port via Web API
  22. echo "$(date): Updating qBittorrent to listen on port $FORWARDED_PORT..." >> "$LOGFILE"
  23.  
  24. # Construct the JSON payload for the API request
  25. JSON_PAYLOAD="{\"listen_port\":$FORWARDED_PORT}"
  26.  
  27. # Make the API request to update qBittorrent's port
  28. RESPONSE=$(curl -s -w "%{http_code}" -o /dev/null -X POST \
  29. -d "json=$JSON_PAYLOAD" \
  30. "$QB_WEBUI_URL/api/v2/app/setPreferences" \
  31. -u "$QB_API_USER:$QB_API_PASS")
  32.  
  33. # Check if the request was successful
  34. if [ "$RESPONSE" -eq 200 ]; then
  35. echo "$(date): Successfully updated qBittorrent to listen on port $FORWARDED_PORT." >> "$LOGFILE"
  36. else
  37. echo "$(date): Failed to update qBittorrent's port. HTTP status code: $RESPONSE" >> "$LOGFILE"
  38. fi
Add Comment
Please, Sign In to add comment