linuxscripts/system-bin/randomvpn

39 lines
966 B
Bash
Executable file

#!/usr/bin/env bash
# Script to select a random VPN client from /etc/openvpn/client/ (the default openvpn directory)
# remember that openvpn profiles in /etc/openvpn/client/ need to be in the format profile.conf
# Example: hk-04.conf
workdir="/etc/openvpn/client/"
isopenvpn=$(command -v openvpn > /dev/null)
issystemd=$(command -v systemctl > /dev/null)
if [ $UID -ne 0 ]; then
echo "You need root privileges."
exit
fi
if $isopenvpn && $issystemd && [ "$1" == "stop" ]; then
systemctl stop openvpn-client@*
exit
fi
if $isopenvpn && $issystemd; then
if [ -d "$workdir" ]; then
cd "$workdir"
vpn=$(shuf -ezn 1 * | xargs -0 -n1)
if [ -f "$vpn" ]; then
vpn=$(echo "$vpn" | cut -f1 -d '.')
systemctl start openvpn-client@"$vpn"
exit
else
echo "$vpn is not a file."
exit
fi
else
echo "$workdir does not exist, leaving."
exit
fi
else
echo "OpenVPN or systemd not installed, exiting."
exit
fi