Skip to content

Instantly share code, notes, and snippets.

@flostellbrink
Last active December 13, 2024 04:09
Show Gist options
  • Save flostellbrink/2d1dc15a0133a0833934459961e8efe8 to your computer and use it in GitHub Desktop.
Save flostellbrink/2d1dc15a0133a0833934459961e8efe8 to your computer and use it in GitHub Desktop.
Script to get new IP addresses in JDownloader by reconnecting Private Internet Access

PIA JDownloader Reconnect

Windows

Script to get new IP addresses in JDownloader by reconnecting Private Internet Access.

  1. Go to Settings -> Reconnect
  2. Tick all three checkboxes.
  3. Set method to External Batch Reconnect
  4. Set interpreter to cmd /c
  5. Set script to
piactl.exe background enable
timeout 3
piactl.exe disconnect
piactl.exe connect
  1. Set application folder to C:\Program Files\Private Internet Access

Mac & Linux

  1. Go to Settings -> Reconnect
  2. Tick all three checkboxes.
  3. Set method to External Batch Reconnect
  4. Set interpreter to /bin/bash -c
  5. Set script to
./piactl background enable
./piactl disconnect
./piactl connect
  1. Set application folder to /usr/local/bin
@marcantoineveilleux
Copy link

marcantoineveilleux commented Dec 9, 2024

Here's my version. I've been running it for 48 hours+ with constant download without any issues. I've also made sure that it logs the last attempt to double check if there are any errors. Simply put the file as reconnect.bat in C:\Program Files\Private Internet Access\

chcp 65001 > nul
setlocal enabledelayedexpansion

:: Define the path to piactl.exe
set "piactl=C:\Program Files\Private Internet Access\piactl.exe"

:: Define the log file path
set "LOGFILE=%~dp0reconnect.log"

:: Start logging
echo [%date% %time%] Script started. > "%LOGFILE%"

:: Enable background operation
echo [%date% %time%] Enabling background operation... >> "%LOGFILE%"
"%piactl%" background enable >> "%LOGFILE%" 2>&1

:: Disconnect from the current VPN session
echo [%date% %time%] Disconnecting VPN... >> "%LOGFILE%"
"%piactl%" disconnect >> "%LOGFILE%" 2>&1

:: Wait for 3 seconds to ensure disconnection
echo [%date% %time%] Pausing for 3 seconds... >> "%LOGFILE%"
ping 127.0.0.1 -n 4 > nul

:: Retrieve the list of available regions
echo [%date% %time%] Retrieving available regions... >> "%LOGFILE%"
set /a count=0
for /f "tokens=*" %%a in ('"%piactl%" get regions') do (
    set /a count+=1
    set "region[!count!]=%%a"
    echo [%date% %time%] Found region: %%a >> "%LOGFILE%"
)

:: Generate a random number between 1 and the number of regions
set /a random_index=%random% %% count + 1
set "random_region=!region[%random_index%]!"

:: Set the VPN region to the randomly selected one
echo [%date% %time%] Setting VPN region to: %random_region% >> "%LOGFILE%"
"%piactl%" set region "%random_region%" >> "%LOGFILE%" 2>&1

:: Connect to the VPN
echo [%date% %time%] Connecting to VPN... >> "%LOGFILE%"
"%piactl%" connect >> "%LOGFILE%" 2>&1

:: Check VPN connection status in a loop
:check_connection
echo [%date% %time%] Checking VPN connection status... >> "%LOGFILE%"
for /f "tokens=*" %%i in ('"%piactl%" get connectionstate') do set state=%%i
echo [%date% %time%] Current state: %state% >> "%LOGFILE%"
if "%state%" neq "Connected" (
    :: Wait for 2 seconds before rechecking
    echo [%date% %time%] Not connected. Pausing for 2 seconds before retrying... >> "%LOGFILE%"
    ping 127.0.0.1 -n 3 > nul
    goto check_connection
)

:: Connection established
echo [%date% %time%] VPN connected to region: %random_region% >> "%LOGFILE%"

:: End of script
echo [%date% %time%] Script completed. >> "%LOGFILE%"
endlocal

@lurkerman837
Copy link

Here's my version. I've been running it for 48 hours+ with constant download without any issues. I've also made sure that it logs the last attempt to double check if there are any errors. Simply put the file as reconnect.bat in C:\Program Files\Private Internet Access\

chcp 65001 > nul
setlocal enabledelayedexpansion

:: Define the path to piactl.exe
set "piactl=C:\Program Files\Private Internet Access\piactl.exe"

:: Define the log file path
set "LOGFILE=%~dp0reconnect.log"

:: Start logging
echo [%date% %time%] Script started. > "%LOGFILE%"

:: Enable background operation
echo [%date% %time%] Enabling background operation... >> "%LOGFILE%"
"%piactl%" background enable >> "%LOGFILE%" 2>&1

:: Disconnect from the current VPN session
echo [%date% %time%] Disconnecting VPN... >> "%LOGFILE%"
"%piactl%" disconnect >> "%LOGFILE%" 2>&1

:: Wait for 3 seconds to ensure disconnection
echo [%date% %time%] Pausing for 3 seconds... >> "%LOGFILE%"
ping 127.0.0.1 -n 4 > nul

:: Retrieve the list of available regions
echo [%date% %time%] Retrieving available regions... >> "%LOGFILE%"
set /a count=0
for /f "tokens=*" %%a in ('"%piactl%" get regions') do (
    set /a count+=1
    set "region[!count!]=%%a"
    echo [%date% %time%] Found region: %%a >> "%LOGFILE%"
)

:: Generate a random number between 1 and the number of regions
set /a random_index=%random% %% count + 1
set "random_region=!region[%random_index%]!"

:: Set the VPN region to the randomly selected one
echo [%date% %time%] Setting VPN region to: %random_region% >> "%LOGFILE%"
"%piactl%" set region "%random_region%" >> "%LOGFILE%" 2>&1

:: Connect to the VPN
echo [%date% %time%] Connecting to VPN... >> "%LOGFILE%"
"%piactl%" connect >> "%LOGFILE%" 2>&1

:: Check VPN connection status in a loop
:check_connection
echo [%date% %time%] Checking VPN connection status... >> "%LOGFILE%"
for /f "tokens=*" %%i in ('"%piactl%" get connectionstate') do set state=%%i
echo [%date% %time%] Current state: %state% >> "%LOGFILE%"
if "%state%" neq "Connected" (
    :: Wait for 2 seconds before rechecking
    echo [%date% %time%] Not connected. Pausing for 2 seconds before retrying... >> "%LOGFILE%"
    ping 127.0.0.1 -n 3 > nul
    goto check_connection
)

:: Connection established
echo [%date% %time%] VPN connected to region: %random_region% >> "%LOGFILE%"

:: End of script
echo [%date% %time%] Script completed. >> "%LOGFILE%"
endlocal

I'm having the weirdest issue. Using PIA with split tunnel if that helps.

This script works almost perfectly. When i test the script under settings > reconnect > reconnect method, it works, however, jdownloader2 says that it doesn't. It's going by my local non-pia IP. For whatever reason it's not looking at my VPN's IP.

I've tried messing with some settings and can't seem to get this to work. If I manually change pia regions it works fine.

Do you have any suggestions? Perhaps we could get on discord/elsewhere to troubleshoot?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment