This Bash script automates connecting to the HTWG Konstanz VPN (OpenVPN with MFA / OTP). It combines your password with a time-based one-time password (TOTP) generated by Python — all in a single command.
The current version stores the password and user as env variable on the system. This is considered not safe. Use this script on one's own responsibility. Feel free to implement an own version with a secured password-manager.
vpn-connect/
├── connecttovpn.sh -> Main script (executes the VPN connection)
├── getotp.py -> Python script that generates the OTP from your TOTP secret
└── vpn_log.txt -> Log file for errors and debug output
-
OTP secret The OTP secret is provided by the HTWG IT department via QR code. For this script, you need the string representation of the secret. You can extract it using tools like extract_otp_secrets.
-
OpenVPN
-
Linux system e.g. Ubuntu, Fedora, Arch, etc. (In theorie MacOs shall also work. This though has not been tested by me, since I have no MacOs instance)
-
Python 3.x with the
pyotppackage installed:To keep dependencies isolated, you can use a virtual environment:
python3 -m venv .venv source .venv/bin/activateThen install package
pyotppip install pyotp
-
Access to the configuration file HTWG-MFA-[current_semester]-STUD.ovpn (provided by HTWG → Link)
The script uses three environment variables for your credentials and the OTP secret:
export VPN_USER="your.htwg.username"
export VPN_PASS="your_htwg_account_password"
export HTWG_TOTP_SECRET="SECRET_FROM_MFA_APP"Tip: You can add these lines to your
.envor~/.bashrcfile to have them loaded automatically on startup.
-
Make the script executable:
chmod +x connecttovpn.sh
-
Run the script:
./connecttovpn.sh
- If not run as root, it will automatically restart with
sudo. - The OTP is generated by
getotp.py. - The OpenVPN connection is established in the same step.
- If not run as root, it will automatically restart with
-
Checks for root privileges → if not, restarts with
sudo. -
Resolves paths relative to the script directory (even when called via symlink).
-
Generates the OTP using the Python script:
python3 getotp.py $HTWG_TOTP_SECRET -
Combines the password and OTP, writes a temporary
auth.txtfile. -
Starts OpenVPN with:
openvpn --config HTWG-MFA-SOSE25-STUD.ovpn --auth-user-pass <(echo -e "$VPN_USER\n$VPN_PASS$OTP") -
Logs any errors to
vpn_log.txt.
If something goes wrong:
cat vpn_log.txtCommon issues:
- Incorrect
HTWG_TOTP_SECRET - Missing
pyotpinstallation openvpnnot found in PATH- Configuration file not found or in wrong directory
export VPN_USER="mmuster"
export VPN_PASS="SuperSecure123"
export HTWG_TOTP_SECRET="JBSWY3DPEHPK3PXP"
./connecttovpn.shExecute this command while being in this repo.
sudo ln -s $PWD/connecttovpn.sh /usr/local/bin/htwgvpnThis way, you can keep your script in its project folder but still run it globally:
htwgvpn