# OpenVPN connection monitor v1.0 # # "OpenVPN connection monitor" monitors a VPN connection. # If the connection is up and working, the client (for example utorrent) will be started. # If the VPN connection is interrupted, the client will be killed and the VPN connection will try to reestablish, then start the client again. # # The script is prepared for monitoring utorrent client. Change the $ClientName and $ClientExecutable variables below to monitor another client. # The script requires at least Windows 7 or Windows Server 2008 R2 to function. For older versions of Windows Powershell 2.0 needs to be installed. # # How to run: # 1. Start Windows PowerShell. # 2: If running a script for the first time, run Windows PowerShell as Administrator and run command: Set-ExecutionPolicy RemoteSigned # 3a: Execute the script: "C:\path\to\script\MonitorVPN.ps1" # 3b: To start the script from a Desktop shortcut: New Shortcut, Target: powershell.exe -file "C:\path\to\script\MonitorVPN.ps1" # Variables. Verify and change if needed $VpnInterfaceName = "tap0" # The display name of the TAP-adapter in Network connections $OpenVpnPath = "C:\Program Files\OpenVPN" # Path to OpenVPN directory $Configfile = "$OpenVpnPath\config\OVPN - Sverige.ovpn" # The name of the OVPN config file. Multiple configs may be used, see function ConnectVPN to enable. $ClientName = "utorrent" # The name of client to start and kill when VPN is up/down. Usually the name of the executable file $Sleeptime = 5 # Number of seconds to wait before running connection status checks again $ClientExecutable = "C:\Program Files (x86)\uTorrent\uTorrent.exe" # Full path to client executable # Function to start the OpenVPN connection function ConnectVPN { # If using several configs in OpenVpnPath, uncomment the below 3 lines to chose a config at random when connecting #$Configs = Get-ChildItem "$OpenVpnPath\config\*.ovpn" #$Configfile = $Configs[(get-random -Minimum 0 -Maximum $Configs.Length)] #Write-host "Using config:" $Configfile.Name -ForegroundColor Cyan # Launch openvpn in a new command prompt window. Wait 40 seconds for connection to establish cd "$OpenVpnPath\bin"; cmd /c start powershell -Command ".\openvpn.exe --config '$Configfile' --mute-replay-warnings; sleep 60" } # Function to disconnect the VPN interface function DisconnectVPN { Get-Process -ProcessName "openvpn" -ErrorAction SilentlyContinue | Stop-Process } # Function to view connection status and VPN interface (Up / Down) function ConnectionStatus { $Connection = get-wmiobject win32_networkadapter -filter "netconnectionid = '$VpnInterfaceName'" Return $Connection.NetEnabled } # Function to display timestamp in output function Timestamp { write-host "$(Get-Date -UFormat "%Y%m%d %H:%M") " -nonewline } Write-host "OVPN monitor started" -BackgroundColor DarkCyan # Begin endless repeated loop while ($true) { # Test if the client is running $ClientRunning = Get-Process "$ClientName" -Erroraction SilentlyContinue # If VPN connection is down, kill the client if ((ConnectionStatus) -ne $true -and $ClientRunning -ne $null) { Timestamp; Write-host "VPN connection is down. Killing $ClientName" -Backgroundcolor DarkRed $ClientRunning | Stop-Process; sleep 5 } # Connect to VPN if not Up if ((ConnectionStatus) -ne $true) { Timestamp; Write-host "Re/connecting to VPN" -Backgroundcolor Blue DisconnectVPN ConnectVPN } # If VPN is up and client is not running, start client if ( (ConnectionStatus) -eq $true -and $ClientRunning -eq $null) { Timestamp; Write-host "Starting $ClientName" -Backgroundcolor DarkGreen Invoke-Item $ClientExecutable } # If VPN connection is Up but connection is hanged, restart connection $Ping = ping google.com -4 -n 1 | where {$_ -like "*TTL=*"} if ((ConnectionStatus) -eq $true -and $Ping -eq $null) { if($Ping -eq $null) { Timestamp; Write-host "VPN connection hung(1). Restarting VPN connection" -Backgroundcolor Blue } else { Timestamp; Write-host "VPN connection hung(2). Restarting VPN connection" -Backgroundcolor Blue } DisconnectVPN ConnectVPN } sleep $Sleeptime }