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
@0x6E75
Copy link

0x6E75 commented Mar 3, 2024

Maybe try this in a batch file inside your PIA folder, triggering it with JDownloader2's "External Tool Reconnect" reconnect method.

@echo off
REM Reconnects PrivateInternetAccess (PIA) VPN.
REM - Waits for PIA to disconnect, then keeps trying to reconnect until PIA is connected.
REM - Plz be gentle, I suck at batch :P - nu
SET vStep=1

piactl.exe background enable
    sleep 1

piactl.exe disconnect
    ECHO Waiting for PIA to disconnect...
    sleep 1

REM This while loop waits for connection states
:check_connection
    REM Check connection state
    FOR /F %%i IN ('piactl.exe get connectionstate') DO SET vState=%%i
    ECHO State=%vState%

    REM Step #1: Keeping checking to see if we are disconnected yet.
    IF %vStep% == 1 (
        IF "%vState%" == "Disconnected" (
            ECHO PIA is now disconnected.
            SET vStep=2
        )
        GOTO :check_connection
    )

    REM Step #2: Keep checking to see if we have reconnected yet.
    IF %vStep% == 2 (
        IF "%vState%" == "Connected" (
            ECHO PIA has reconnected.
            GOTO :end
        )
        ECHO Attempting to reconnect...
        piactl.exe connect
        sleep 3
        GOTO :check_connection
    )

:end

If I paste this script into JDownloader2>Settings>Reconnect>Reconnect Method>"External Batch Reconnect" and set "Start in" to "C:\Program Files\Private Internet Access" then this seems to work better, but then connecting still gets stuck about 10% of the time. Folder/app permissions don't seem to matter.

If switch Reconnect Method to "External Tool Reconnect", save this exact same script into a batch file inside my PIA direcotry, then set Reconnect Method > Command > "C:\Program Files\Private Internet Access\reconnect.bat", the reconnection seems more reliable for some reason.

--
For noobs: How-to save this code into a batch file inside your PIA directory on Windows 11

  • Enable permission to add files to the PIA directory: In Windows 11, start File Explorer, go to "C:\Program Files", right-click on the "Private Internet Access" directory, then select "Properties". Go to the Security tab, find the top area labeled "Group or user names:", scroll down to "Users", then click "Edit...". In the new window that pops up, again find the top area labeled "Group or user names:", scroll down to "Users", then in the bottom area check the Allow box next to "Full control". Click Apply, then OK. Then OK again.
  • Create a new empty bat file: Now open the "C:\Program Files\Private Internet Access" directory, Right-click > New > Text Document. Rename the file from "New Text Document.txt" to "reconnect.bat". NOTE: The file's icon should change to a Command Prompt icon with gears on it. If the icon still looks like a notepad then you should google how to turn on file extensions and make the file is not named something like "reconnect.bat.txt" on accident.
  • Save our script into the empty bat file: Right-click "reconnect.bat" and select "Edit in Notepad". Copy the code above, paste into "reconnect.bat" and then save.
  • Set up JDownloader: In JDownloader2 (mine is the latest update as-of March 3, 2024), go to the Settings tab, then Settings. Find "Reconnect" on the left. Set "Reconnect Method" to "External Tool Reconnect", "Command (use absolute directory paths)" to "C:\Program Files\Private Internet Access\reconnect.bat", and make sure "Parameter (1 parameter per line)" is empty. You can now close the settings window.
  • Test your connection by clicking the reconnect button. Cross your fingers.

@EamonHendricks
Copy link

EamonHendricks commented Mar 5, 2024

Thank you so much for the link. You made my day. I also want to know it and I am glad I found your post. While searching for it online, I also found https://ukwritings.com/write-my-dissertation website link where I found an essay writer who will write my dissertation for me.

@gigovx
Copy link

gigovx commented Mar 9, 2024

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.

@overdrive80
Copy link

overdrive80 commented Sep 21, 2024

Imagen2

@echo off
chcp 65001 > nul
setlocal enabledelayedexpansion

set "pia=C:\Program Files\Private Internet Access\piactl.exe"

rem Obtener las regiones disponibles y almacenarlas en una lista
set contador=0
for /f "tokens=*" %%a in ('cmd /c "%pia%" get regions') do (
    set /a contador+=1
    set "linea[!contador!]=%%a"
)

rem Generar un número aleatorio entre 1 y el número de elementos en el array
set /a random_index=%RANDOM%%%contador + 1

rem Seleccionar el elemento aleatorio del array
set seleccionada=!linea[%random_index%]!

echo La región seleccionada aleatoriamente es: !seleccionada!

rem Habilitar el modo background
"%pia%" background enable

rem Desconectar si ya está conectado
"%pia%" disconnect

"%pia%" set region "!seleccionada!"

"%pia%" connect
pause

@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