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
@lurkerman837
Copy link

Thought I'd add to this as I needed new IP address/location to download multiple files and simply reconnecting to the same VPN location didn't rotate my IP address;

setlocal enabledelayedexpansion

:: Get list of PIA regions into a temporary file
piactl get regions > c:\tmp_regions.txt

:: Initialize variables
set /a count=0
set /a randomLine=0

:: Count how many lines (regions) are in the file
for /F %%A in ('type c:\tmp_regions.txt ^| find /c /v ""') do set /a count=%%A

:: Generate a random number between 1 and the count of regions
set /a randomLine=%random% %% count + 1

:: Initialize loop variables
set /a currentLine=0

:: Read through the file until the random line is reached
for /F "tokens=*" %%A in (c:\tmp_regions.txt) do (
    set /a currentLine+=1
    if !currentLine!==!randomLine! (
        set RANDOMREGION=%%A
        goto connectVPN
    )
)

:connectVPN
:: Enable background connection
piactl background enable

:: Debug: Show the chosen region
echo Chosen region: !RANDOMREGION!

:: Trim and set the region to the randomly selected one
:: The next line is crucial: It removes any potential carriage return in the variable
:: which can be introduced by the way Windows handles text files
for /f "delims=" %%i in ("!RANDOMREGION!") do set RANDOMREGION=%%i

piactl set region !RANDOMREGION!

:: Connect to PIA
piactl connect

:: Clean up
del tmp_regions.txt

echo Connected to region: !RANDOMREGION!
endlocal

This script creates a temporary text file in root of the c drive (due to permissions needed to write it in the PIA folder) that outputs all the locations available to use, it then reads one of the locations randomly and connects you to one.

HOWEVER, when I tried to do this with Jdownloaders 'External Batch Reconnect' option it failed even though when running it as a batch script it works, for some reason it fails to use a random location and reconnects to the same location each time, so instead I copied the above into a bat file inside the PAI folder called 'random' and use Jdownloaders 'External Tool Reconnect' and then select the batch file in the PIA folder and it works! Annoyingly it does run the batch over the top of windows but I use this script on a headless box so it's no issue for me.

I'm a fairly noob user. I would like this script to work and change regions so I can get a new ip. May you please explain how I would use this script from a-z? I added it as a .bat file in the PIA directory and used the "External Tool Reconnect" and pointed to the bat file. It doesn't seem to work.

What should I do? This is the exact functionality I want.

@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