Running Scripts on OpenVPN Connect and Disconnect
Running OpenVPN Client for Windows with script file allows you to create the network drives to share on servers when starting VPN connections. There are other options, such as running login scripts, printer redirects, or automated updating. This tutorial shows the installation and application of OpenVPN connection scripts.
How to do it
The OpenVPN GUI for Windows can be downloaded from the community website here. During installation, the OpenVPN directory is created under
%USERPROFILE%, including the folder config, which contains the configuration files (
.ovpn) that are stored from the OpenVPN GUI with File Import.
Right-clicking above the OpenVPN
The batch files must be in the same config directory and have the same name as the file name of the configuration (.ovpn), whereby _up must be added, as here the file name starts with VPNConfig and appended _up, VPNConfig_up.bat.
The contents of the connection-building batch file may look like this so that a network drive is mapped to the server after connecting to OpenVPN.
It is mapped with
VPNConfig_up.bat the network drive I: to the server
tower, here in this example with domains login
city.local as user
user with password
password. If you do not want to have a visible password in the batch, you remove it, the password prompt will called.
After disconnecting the OpenVPN connection, the batch
VPNConfig_down.bat disconnects the network drives.
The batch files must be in the same directory, here in the user profile under OpenVPN along with the ovpn file in the config directory.
If you rename the configuration file before importing, here VPNConfig.ovpn i.e. CityOffice.ovpn, then this name appears in the context menu when connecting to destination in the OpenVPN GUI. The batch files must have the same names before _up and _down.
In order for the server name to be resolved at
net use, the entry
dhcp-option DNS has to modify in the file VPNConfig.ovpn.
dhcp-option DNS 192.168.3.110
In this example, the DNS 192.168.3.110 is on the remote network to which we connect to OpenVPN. If there is no DNS on the network, the IP address must be specified instead of the host name.
In the OpenVPN settings you will find the possibility to determine the location of the configuration files and log files, here the path to the files can be changed, also there is the possibility to determine the extension (
.ovpn). Adjusting the paths can be useful, for example, when using roaming profiles, creating the folder for the OpenVPN connections under
%APPDATA%, or when using offline folders, this can be set individually.
In the OpenVPN settings, the values for script files can be changed if necessary, in the
Script Timeout area for
Preconnect script timeout, in this case the batch file VPNConfig_up.bat, and VPNConfig_down.bat for timeout for Disconnect script.
Example of a Netlogon Connection Script (xxxxxxxx_up.bat):
REM mapping network printer
rundll32 printui.dll,PrintUIEntry /in /n \\tower\laserjet_office
REM mapping network drive
net use I: \\tower\office /persistent:no /user:city\max pass123
REM wait along two pings
ping localhost -n 2 > nul
REM pop-up window greeting
echo msgbox"Welcome %USERNAME% on %USERDOMAIN%.",vbInformation , "Message"> %temp%\msg.vbs
REM delete message