You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

29 lines
1.0 KiB
Bash

#!/usr/bin/env bash
ovpn_directory="/etc/openvpn/client/ovpn-enabled"
current_ovpn_path="/etc/openvpn/client/current.ovpn"
# TODO: Investigate better ways to check for rootness. This is just a courtesy message, though.
# Non-root users will not be able to do anything with this anyway.
if [ "$(whoami)" != "root" ]; then
echo "This script needs root privileges to interact with the openvpn service."
exit
fi
prior_ovpn_path="$(readlink "${current_ovpn_path}")"
# TODO: Add the ability to pass arbitrary ovpn files.
# TODO: Remove the current config from the list of choices.
# Choose a random VPN configuration inside /etc/openvpn/ovpn-enabled
new_ovpn_path=$(shuf -n1 -e ${ovpn_directory}/*)
echo "OpenVPN is currently connected to $(basename "${prior_ovpn_path}" .ovpn)"
echo "Switching to OpenVPN configuration $(basename "${new_ovpn_path}" .ovpn)"
ln -fs "${new_ovpn_path}" "${current_ovpn_path}"
echo "Restarting OpenVPN service."
systemctl restart openvpn
# TODO: Add a way to report any errors. Look at systemctl is-active and is-failed.