qBittorrent and PIA port forward automation script and directions

Windows specific questions, problems.
Post Reply
RevanT

qBittorrent and PIA port forward automation script and directions

Post by RevanT »

I've finally worked out how to automate port forwarding and PIA.

I've setup a Task Scheduler task that triggers on event.
Log: Microsoft-Windows-NetworkProfile/Operation
Source: NetworkProfile
Event ID: 10000

This event happens when PIA connects or reconnects.

Have the action set to start a program and point to the script created from the script template below.


Find your qbittorrent.ini file usually in %userprofile%\AppData\Roaming\qBittorrent\
Copy qbittorrent.ini to qbittorrent1.ini
Edit qbittorrent1.ini and replace line Connection\PortRangeMin=(your port number) with 123456

example of my ini before and after:

Before:
DynDNS\DomainName=changeme.dyndns.org
Connection\PortRangeMin=31081
Bittorrent\MaxConnecs=-1

After:
DynDNS\DomainName=changeme.dyndns.org
123456
Bittorrent\MaxConnecs=-1

Save qbittorrent1.ini


Script to close qbittorrent, add 5 sec wait, pull the PIA port forward number, delete qbittorrent.ini, create new qbittorrent.ini with the port forward info added; using qbittorrent1.ini as a source file, add 5 sec wait, and then restart qbittorrent.


@echo off
taskkill /IM "qbittorrent.exe" /F
PING localhost -n 5 >NUL

C:\Progra~1\PRIVAT~1\piactl get portforward > %userprofile%\AppData\Roaming\qBittorrent\port.txt
set /p port= < %userprofile%\AppData\Roaming\qBittorrent\port.txt
del %userprofile%\AppData\Roaming\qBittorrent\port.txt /q
del %userprofile%\AppData\Roaming\qBittorrent\qbittorrent.ini /q

set "replace=123456"
set "replaced=Connection\PortRangeMin=%port%"

set "source=qbittorrent1.ini"
set "target=qbittorrent.ini"

setlocal enableDelayedExpansion
(
for /F "tokens=1* delims=:" %%a in ('findstr /N "^" %source%') do (
set "line=%%b"
if defined line set "line=!line:%replace%=%replaced%!"
echo(!line!
)
) > %target%
endlocal

PING localhost -n 5 >NUL
start C:\Progra~1\qBittorrent\qbittorrent.exe
exit
Post Reply