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/bashcd ~/vpnssudo openssl aes-128-cbc -d < login.conf.aes > login.confcd ~/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 ~/vpnssleep 20wipe -f login.confOpenssl 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?
Connecting 3 OpenVPN connections through a shell script
bash;shell script;openvpn
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/bashVPNDIR=${HOME}/vpnsCONFIG=${VPNDIR}/login.confOCWD=${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) &doneunset CREDScd ${OCWD}