@tomwork Thank you for sharing this great script, we have the same problem with the AWS tunnels ;-)
We have a CARP HA setup and wanted to have it on both nodes.
Therefore we need a check, that the script only starts down tunnels only if the CARP state is MASTER and is not active on the BACKUP node.
Here it is - there may be better solutions but it works
#!/bin/sh
# check for MASTER
master=`ifconfig | grep "carp: MASTER"`
if [ -z "$master" ]; then
echo "CARP Backup => exit script"
exit;
fi
echo "CARP Master verifying IPSec tunnels..."
tunnels=$( /usr/local/sbin/ipsec statusall | /usr/bin/grep dpddelay | /usr/bin/cut -d':' -f1 | /usr/bin/tr -d ' ' )
for i in $tunnels; do
if /usr/local/sbin/ipsec status $i | /usr/bin/grep -q 'no match'; then
echo "tunnel $i down"
/usr/local/sbin/ipsec up $i
fi