Providing password to openconnect
I'm using openconnect to connect to my university VPN, and until today I was using it through these bash functions, in my .bashrc file:
function startvpn() {
AUTHGROUP="My group name"
HOST=vpn.server.example.com
sudo /usr/sbin/openconnect --background \
--pid-file="${HOME}/.openconnect.pid" \
--authgroup="${AUTHGROUP}" \
--user=${USER} \
${HOST}
function stopvpn() {
if [[ -f "$HOME/.openconnect.pid" ]]; then
sudo kill -2 $(cat "${HOME}/.openconnect.pid") && rm -f "${HOME}/.openconnect.pid"
else
echo "openconnect pid file does not exist !"
fi
}
sudo may ask for my local password, and openconnect will then ask for my global (let's say LDAP) password, which is a bit complex (well, is has to be), so it's not extremely convenient but that was ok since I was not using the VPN very much.
However last few days I have realized that access to the university SMTP server (for outgoing mail) is now restricted to IPs in the university network, or through the VPN. Note this is something I deduced from my own experiments trying to find out why I suddenly couldn't send mail any more. I'm using mu4e in emacs for my mail so I'm probably one of the very few persons who were affected, certainly the vast majority of people use the webmail anyway nowadays.
Still, I now have to bring the VPN up if I want to send mail when I'm not physically on site (except if I use the webmail, obviously, and while I can do that occasionally, I certainly don't want to switch to that as my main interface).
So I looked for a way to automatically provide the password to openconnect. It turns out this is very simple, just use the --passwd-on-stdin option and ensure the password gets indeed written there.
Here's the updated function to bring the VPN up:
function startvpn() {
AUTHGROUP="My group name"
HOST=vpn.server.example.com
pass show VPNpasswd | sudo /usr/sbin/openconnect --background \
--pid-file="${HOME}/.openconnect.pid" \
--authgroup="${AUTHGROUP}" \
--user=${USER} \
--passwd-on-stdin \
${HOST}
}
pass is the "standard unix password manager" I'm already using for all my password needs.