Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # Config variables
- QB_WEBUI_URL="http://qbittorrent:port"
- QB_API_USER="redacted"
- QB_API_PASS="redacted"
- LOGFILE="/directory/to/save/port_update.log"
- # Keep only the last 150 lines
- tail -n 150 "$LOGFILE" > "${LOGFILE}.tmp" 2>/dev/null && mv "${LOGFILE}.tmp" "$LOGFILE"
- # Get the forwarded port from PIA
- FORWARDED_PORT=$(piactl get portforward)
- # Check if the forwarded port is valid
- if [[ "$FORWARDED_PORT" == "Inactive" || "$FORWARDED_PORT" == "Unavailable" ]]; then
- echo "$(date): PIA port forwarding is not available." >> "$LOGFILE"
- exit 1
- fi
- # Update qBittorrent's listening port via Web API
- echo "$(date): Updating qBittorrent to listen on port $FORWARDED_PORT..." >> "$LOGFILE"
- # Construct the JSON payload for the API request
- JSON_PAYLOAD="{\"listen_port\":$FORWARDED_PORT}"
- # Make the API request to update qBittorrent's port
- RESPONSE=$(curl -s -w "%{http_code}" -o /dev/null -X POST \
- -d "json=$JSON_PAYLOAD" \
- "$QB_WEBUI_URL/api/v2/app/setPreferences" \
- -u "$QB_API_USER:$QB_API_PASS")
- # Check if the request was successful
- if [ "$RESPONSE" -eq 200 ]; then
- echo "$(date): Successfully updated qBittorrent to listen on port $FORWARDED_PORT." >> "$LOGFILE"
- else
- echo "$(date): Failed to update qBittorrent's port. HTTP status code: $RESPONSE" >> "$LOGFILE"
- fi
Add Comment
Please, Sign In to add comment