Reconnect VPN on connection loss using NetworkManager’s nmcli

Find the <UUID> of your VPN connection using:

nmcli connection show

Using nmcli you can (re-)connect to your VPN by:

nmcli connection up uuid 

Checking every 10 seconds, if VPN is still up, and reconnect otherwise:

#!/bin/bash +x
UUID="<UUID>"
while (true)
do
        VPNCON=$(nmcli connection show --active | grep -i vpn | grep -i "${UUID}" | cut -f3 -d " ")
        if [[ $VPNCON != "${UUID}" ]] # Double check
        then
                nmcli connection up uuid "${UUID}"
        fi
        sleep 10
done