Jump to content
  • 1

Script to switch personalised torguard .ovpn configurations

Rate this question



On my server I have four torguard configs and like to switch between them without effort when I want to change location, change encryption, or open ports.


Using an alias and a single letter (e.g. "tor l") is enough to construct a handcrafted config from my personalised bits and a standard config file that has been downloaded and renamed correctly.


Rather than maintain customised config files I thought I'd write a script as an exercise.


I haven't added functionality to reload openvpn as I have aliases "ton" and "toff".  I don't know bash scripting that well, suggestions welcome.


# torGuard script to list torguard *.ovpn files or build a new torguard.conf including
# personalised bits such as a password file and dns changer

# downloaded torguard *.ovpn files should be renamed to something decriptive with a
# unique initial letter e.g. london.ovpn, newjersey.ovpn, openport.ovpn (bittorrent)

# usage:
# script is run by an alias tor="sudo torGuard"
# "tor": list the available torguard config file names with server and exit
# "tor [initial search]": build a new config file based on matching initial letter

MYIDENT="# Torguard configuration via"

# show the current server

echo ""
grep -e "$MYIDENT" $CONF|cut -b3-

# find: e.g. "tor n" would match newjersey.ovpn, "tor l" for london.ovpn

OVPN=`find $OPENVPNDIR -iname "$1*.ovpn"`

# two or more matches will be separated by space (e.g. naming error: non-unique first
# letter, or input error "tor *o")

FOUNDSPACE=`echo $OVPN|grep -c " "`

# if anything other than one match, list the available .ovpn files then exit

if [ "$OVPN" == "" ] || [ "$FOUNDSPACE" == "1" ] ; then
    echo -e "\nAvailable:"
    find $OPENVPNDIR/*.ovpn -exec basename -s .ovpn {} +

# otherwise build torguard.conf starting with the personalised bits

BASENAME=`basename -s .ovpn $OVPN`

cat > $CONF << STOP
auth-user-pass $OPENVPNDIR/userpass.auth
script-security 2

# and then append lines from the matching .ovpn (except for the unwanted
# blank "auth-user-pass" line)

grep -v ^auth-user-pass $OVPN >> $CONF

# display the new config name and server

echo -e "Now $BASENAME ("`grep "^remote " $CONF | cut -d" " -f2`")"

Link to post
Share on other sites

1 answer to this question

Recommended Posts

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...