Forked from tyvsmith/sync-qbittorrent-gluetun-port-forward.sh
Last active
October 5, 2025 10:59
-
-
Save Marsu31/33555a21f5a5331618b6d85417f4e946 to your computer and use it in GitHub Desktop.
TrueCharts - Sync QBittorrent Gluetun Port Forwards
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # Run on a cron from the host. | |
| NAMESPACE=ix-qbittorrent | |
| CONTAINER_VPN=qbittorrent-vpn | |
| CONTAINER_BITTORRENT=qbittorrent | |
| # Fetch the name of the pod with the label "app.kubernetes.io/instance=qbittorrent" | |
| POD=$(k3s kubectl -n $NAMESPACE get pod -l app.kubernetes.io/instance=qbittorrent -o jsonpath="{.items[0].metadata.name}") | |
| # Check the status of the pod | |
| POD_STATUS=$(k3s kubectl -n $NAMESPACE get pod $POD -o jsonpath="{.status.phase}") | |
| if [[ $POD_STATUS != "Running" ]]; then | |
| echo "Pod is not in the Running state, exiting..." | |
| exit 1 | |
| fi | |
| # Get the forwarded port from the file inside the qbittorrent-vpn container | |
| NEW_PORT=$(k3s kubectl exec -n $NAMESPACE $POD -c $CONTAINER_VPN -- cat /tmp/gluetun/forwarded_port) | |
| if [ -z "$NEW_PORT" ]; then | |
| echo "No port found in /tmp/gluetun/forwarded_port" | |
| exit 1 | |
| fi | |
| LAST_PORT=$(k3s kubectl exec -n $NAMESPACE $POD -c $CONTAINER_BITTORRENT -- sh -c "/usr/bin/curl -s 'http://localhost:8080/api/v2/app/preferences' | jq .listen_port") | |
| if [[ $NEW_PORT == $LAST_PORT ]]; then | |
| echo "Port has not changed, exiting..." | |
| exit 0 | |
| else | |
| # Execute the curl request in the qbittorrent container and check response | |
| RESPONSE=$(k3s kubectl exec -n $NAMESPACE $POD -c $CONTAINER_BITTORRENT -- sh -c "/usr/bin/curl -s -o /dev/null -w '%{http_code}' 'http://localhost:8080/api/v2/app/setPreferences' -d 'json={\"listen_port\": \"$NEW_PORT\"}'") | |
| if [ "$RESPONSE" -ne 200 ]; then | |
| echo "Curl request failed with HTTP response code $RESPONSE" | |
| exit 1 | |
| fi | |
| echo "Port updated to $NEW_PORT" | |
| fi |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Fork from https://gist.github.com/tyvsmith/c1b2d0c475502bea87f0f67cc4cd0a5b.
Added: call qbittorrent API to know the current listen port value.