# Using Plex Server with a VPN

**URL:** <https://forums.plex.tv/t/using-plex-server-with-a-vpn/31548>\
**Category:** General Discussions\
**Created:** [March 27, 2013, 7:31am UTC](https://forums.plex.tv/t/using-plex-server-with-a-vpn/31548 "2013-03-27T07:31:48Z")\
**Posts on this page:** 1\
**Showing post:** 45

<div class="post-metadata">

**Author:** ![XFlak](https://avatars.discourse-cdn.com/v4/letter/x/da6949/32.png) [@XFlak](https://forums.plex.tv/u/XFlak)\
**Post date:** [March 16, 2014, 6:10pm UTC](https://forums.plex.tv/t/using-plex-server-with-a-vpn/31548/45 "2014-03-16T18:10:16Z")

</div>

~Updated June 17, 2015~  
  
I wrote a windows batch file to route plex.tv's current IP address (with various subnet masks to choose from) to your machine's default gateway (e.g. 192.168.2.1). I promise the bat file is safe, just google me and my reputation will speak for itself.  
  
The code is below but I also uploaded a zip containing the bat file and an exe version of the bat file (same as the bat file except runs invisibly). For best results, use windows task scheduler to launch the exe (with highest privileges) as often as you'd like.  
  
This will ensure that even if the IP for plex.tv changes, your setup will automatically route it past your VPN. A list of routed IPs will be saved here:  
"%userprofile%\AppData\Local\Plex Media Server\PermittedPlexIPs.txt"  
  
I'm using PIA and this works perfectly.  
  
Enjoy!  
  
XFlak  
  
P.S.  
  
I forgot to mention. Check out my other projects at [https://xflak40.wordpress.com](https://xflak40.wordpress.com)  
  
Tons of good stuff there, as well as donation links should you like my work and be feeling generous  
  
&nbsp;

```
@echo off
setlocal
set PATH=%SystemRoot%\system32;%SystemRoot%\system32\wbem;%SystemRoot%
chcp 437>nul

echo VPN Bypass for Plex Media Server  
echo by XFlak  
echo.

::get Default Gateway  
ipconfig|findstr /I /C:“Default Gateway”|findstr /I /C:“1” \>"%temp%\gateway.txt"  
set /p gateway= \<"%temp%\gateway.txt"  
set gateway=%gateway:\*: =%  
::echo %gateway%  
::If gateway is detected incorrectly, override it by uncommenting the below like (delete : and input your correct gateway  
::set gateway=192.168.2.1

echo Getting [plex.tv](http://plex.tv) current IP addresses…  
echo.  
echo Note: Log of [plex.tv](http://plex.tv)’s routed IP’s saved here:  
echo %userprofile%\AppData\Local\Plex Media Server\PermittedPlexIPs.txt  
echo.  
nslookup “[plex.tv](http://plex.tv)”|findstr /I /V “Server: Address: Name: timeout” \>"%temp% emp.txt"  
findstr /I /C:" &nbsp;" “%temp% emp.txt” \>"%temp%\plex.tv.txt"  
echo.

cd /d “%temp%”  
for /F “tokens=\*” %%A in (plex.tv.txt) do call :list %%A  
goto:donelist

:list  
set PlexIP=%\*  
set PlexIP=%PlexIP:\* &nbsp;=%  
echo %PlexIP%  
if not exist “%userprofile%\AppData\Local\Plex Media Server\PermittedPlexIPs.txt” goto:skipcheck

findstr /I /C:"%PlexIP%" “%userprofile%\AppData\Local\Plex Media Server\PermittedPlexIPs.txt”\>nul  
IF NOT ERRORLEVEL 1 (echo IP already routed, skipping…) & (goto:EOF)  
:skipcheck

echo route -p add %PlexIP% mask 255.255.255.255 %gateway%  
route -p add %PlexIP% mask 255.255.255.255 %gateway%  
echo.  
echo %PlexIP% \>\>"%userprofile%\AppData\Local\Plex Media Server\PermittedPlexIPs.txt"  
goto:EOF

:donelist

::clean no longer used IPs  
echo.  
echo Removing routed IPs no longer used by [plex.tv](http://plex.tv)  
echo.

if exist “%userprofile%\AppData\Local\Plex Media Server\PermittedPlexIPs2.txt” del “%userprofile%\AppData\Local\Plex Media Server\PermittedPlexIPs2.txt”\>nul  
if not exist “%userprofile%\AppData\Local\Plex Media Server” goto:doneclean  
if not exist “%userprofile%\AppData\Local\Plex Media Server\PermittedPlexIPs.txt” goto:doneclean

cd /d “%userprofile%\AppData\Local\Plex Media Server”

for /F “tokens=\*” %%A in (PermittedPlexIPs.txt) do call :clean %%A  
goto:doneclean

:clean  
set PlexIP=%\*  
findstr /I /C:"%PlexIP%" “%temp%\plex.tv.txt” \>nul  
IF ERRORLEVEL 1 goto:remove

echo IP still used: %PlexIP%  
echo %PlexIP% \>\>"%userprofile%\AppData\Local\Plex Media Server\PermittedPlexIPs2.txt"  
goto:EOF

:remove  
echo IP no longer used: route delete %PlexIP%  
route delete %PlexIP%  
goto:EOF

:doneclean  
if exist “%userprofile%\AppData\Local\Plex Media Server\PermittedPlexIPs.txt” del “%userprofile%\AppData\Local\Plex Media Server\PermittedPlexIPs.txt”\>nul  
if exist “%userprofile%\AppData\Local\Plex Media Server\PermittedPlexIPs2.txt” move /y “%userprofile%\AppData\Local\Plex Media Server\PermittedPlexIPs2.txt” “%userprofile%\AppData\Local\Plex Media Server\PermittedPlexIPs.txt”\>nul

echo.  
echo Finished, exiting…  
@ping 127.0.0.1 -n 3 -w 1000\> nul  
exit
```

edit: code was missing a "\>", old version still worked but log only retained latest IP, the 4 people who downloaded the last version may want to redownload the new one  
  
edit2: updated script\attachments on May 12, 2015, it's now more efficient\secure by using nslookup instead of ping and using subnet mask 255.255.255.255 instead of 255.255.0.0. Thanks to John Doe at cutting cords for the suggestions.  
  
edit3: updated on May 20, 2015, now includes a read me, an uninstaller, and multiple versions of the script for different subnet masks. Try them in the following order until you find one that works for you. Warning, the further you get to the bottom of the list the more IPs will bypass your VPN.  
  
255.255.255.255  
255.255.255.252  
255.255.255.0  
255.255.0.0  
255.0.0.0  
  
edit4: updated on June 16, 2015, now it removes old IP addresses no longer used by plex.tv from being routed past your VPN. So only the current IP addresses used by plex.tv will bypass your VPN and nothing else.

edit5: minor update, also added version # (v5) and icon to the exe's

---

_[View the full topic](https://forums.plex.tv/t/using-plex-server-with-a-vpn/31548)._
