Skip to content

Instantly share code, notes, and snippets.

@flostellbrink
Last active November 4, 2024 11:00
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.

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