1

For work I need to connect to 3 different OpenVPN connections every day. I'm using a shell script to speed this up, but I feel like it could be done better.

Currently my script is:

#!/bin/bash

cd ~/vpns
sudo openssl aes-128-cbc -d < login.conf.aes > login.conf

cd ~/vpns/live_vpn
(sudo openvpn --config config.ovpn --auth-user-pass ~/vpns/login.conf) &
cd ~/vpns/mgmt_vpn
(sudo openvpn --config config.ovpn --auth-user-pass ~/vpns/login.conf) &
cd ~/vpns/test_vpn
(sudo openvpn --config config.ovpn --auth-user-pass ~/vpns/login.conf) &

cd ~/vpns
sleep 20
wipe -f login.conf

Openssl decrypts a file containing the username and password for the connections, which is then wiped after the connections are established.

This works but has some problems:

  • The repeated cds seem inelegant.
  • I hope the sleep command (which gives openvpn time to use the decrypted login file) can be replaced so that the wipe occurs as soon as the connections are established.
  • I've called sudo above the openvpn lines as otherwise they seem to 'pile up' on the shell and give no opportunity to enter the password.

My question is: What would be a better way to achieve this?

1

I'm not 100% sure about whether or not storing the username/password combination in a variable will work - it definitely won't if your sudo configuration requires a password, but at the least you can implement a loop similar to what's below. If storing the username/password in the variable does work, you won't need to write a file with your credentials at all.

#!/bin/bash
VPNDIR=${HOME}/vpns
CONFIG=${VPNDIR}/login.conf
OCWD=${PWD}

cd ${VPNDIR}
OLDIFS=${IFS} ; IFS=''
CREDS="$(openssl aes-128-cbc -d < login.conf.aes)"
IFS=${OLDIFS}

for Z in live_vpn mgmt_vpn test_vpn; do
   cd ${VPNDIR}/${Z}
   (printf "${CREDS}\n" | sudo openvpn --config config.ovpn --auth-user-pass) &
done

unset CREDS
cd ${OCWD}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.