Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
OpenVPN Connection Management

This gist contains a Windows task that will automatically connect to the home.ovpn network when connecting to any Wi-Fi network other than "Peace", and another task that will disconnect from the OpenVPN whenever you disconnect from Wifi.

Installation Instructions

  1. Download the xml files from this gist.
  2. Modify the Exec Arguments at the bottom of the Connection file to work with your network configuration by:
    1. Replacing "Peace" with your home Wi-Fi network name
    2. Replacing "home.ovpn" with the name of the ovpn network config file you wish to use.
  3. Open "Task Scheduler"
  4. Use the "Import task..." action and choose the files you downloaded.

No Popups

Windows will display the command dialog while the commands are executing. This can be avoided with some additional system configuration.

  1. Open Task Scheduler
  2. Open Properties for both tasks
    1. Click "Change Users or Groups"
    2. Type in System
    3. Click "Check Names"
    4. Click OK
  3. If you have a username and password for your account:
    1. Open your ovpn profile. It can be found in your user directory under OpenVPN\config\profilename\
    2. Find the line: auth-user-pass
    3. Add "credentials.txt" to the end of that line
    4. Save your changes
    5. Create a file in the same directory called "credentials.txt"
    6. Add your username to the first line of the file, and your password to the second line of the file
      I know this seems unsafe, but only system process and system admins will be able to access this file after you move it at the next step.
  4. Copy/Move the OpenVPN folder in your UserProfile, to C:\WINDOWS\system32\config\systemprofile
  5. Run the connect task
  6. Check the log file in C:\WINDOWS\system32\config\systemprofile\OpenVPN\logs to see if it connected successfully
<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<RegistrationInfo>
<Date>2018-12-20T17:30:53.9213074</Date>
<Author>Carlin Scott</Author>
<URI>https://gist.github.com/carlin-q-scott</URI>
</RegistrationInfo>
<Triggers>
<EventTrigger>
<Enabled>true</Enabled>
<Subscription>&lt;QueryList&gt;&lt;Query Id="0" Path="Microsoft-Windows-WLAN-AutoConfig/Operational"&gt;&lt;Select Path="Microsoft-Windows-WLAN-AutoConfig/Operational"&gt;*[System[EventID=8001]]&lt;/Select&gt;&lt;/Query&gt;&lt;/QueryList&gt;</Subscription>
</EventTrigger>
</Triggers>
<Settings>
<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
<DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
<StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
<AllowHardTerminate>true</AllowHardTerminate>
<StartWhenAvailable>false</StartWhenAvailable>
<RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
<IdleSettings>
<StopOnIdleEnd>true</StopOnIdleEnd>
<RestartOnIdle>false</RestartOnIdle>
</IdleSettings>
<AllowStartOnDemand>true</AllowStartOnDemand>
<Enabled>true</Enabled>
<Hidden>false</Hidden>
<RunOnlyIfIdle>false</RunOnlyIfIdle>
<WakeToRun>false</WakeToRun>
<ExecutionTimeLimit>PT72H</ExecutionTimeLimit>
<Priority>7</Priority>
</Settings>
<Actions Context="Author">
<Exec>
<Command>cmd</Command>
<Arguments>/c netsh wlan show interfaces | findstr /r /c:"Peace" || start /b "" "C:\Program Files\OpenVPN\bin\openvpn-gui.exe" --connect home.ovpn</Arguments>
</Exec>
</Actions>
</Task>
<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<RegistrationInfo>
<Date>2018-12-20T17:30:53.9213074</Date>
<Author>Carlin Scott</Author>
<URI>https://gist.github.com/carlin-q-scott</URI>
</RegistrationInfo>
<Triggers>
<EventTrigger>
<Enabled>true</Enabled>
<Subscription>&lt;QueryList&gt;&lt;Query Id="0" Path="Microsoft-Windows-WLAN-AutoConfig/Operational"&gt;&lt;Select Path="Microsoft-Windows-WLAN-AutoConfig/Operational"&gt;*[System[EventID=8003]]&lt;/Select&gt;&lt;/Query&gt;&lt;/QueryList&gt;</Subscription>
</EventTrigger>
</Triggers>
<Settings>
<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
<DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
<StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
<AllowHardTerminate>true</AllowHardTerminate>
<StartWhenAvailable>false</StartWhenAvailable>
<RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
<IdleSettings>
<StopOnIdleEnd>true</StopOnIdleEnd>
<RestartOnIdle>false</RestartOnIdle>
</IdleSettings>
<AllowStartOnDemand>true</AllowStartOnDemand>
<Enabled>true</Enabled>
<Hidden>false</Hidden>
<RunOnlyIfIdle>false</RunOnlyIfIdle>
<WakeToRun>false</WakeToRun>
<ExecutionTimeLimit>PT72H</ExecutionTimeLimit>
<Priority>7</Priority>
</Settings>
<Actions Context="Author">
<Exec>
<Command>cmd</Command>
<Arguments>/c taskkill.exe /F /IM openvpn*</Arguments>
</Exec>
</Actions>
</Task>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.