Previously I posted a script I used to update my RADIUS server IP (dynamic) on my RADIUS client whenever the IP changes. It was working well until about an hour ago, when apparently CloudFlare’s API endpoint died and the NEW_IP I was getting was only the client’s due to grepping the API response for an IP address, which the 502 page unhelpfully provides (in this case). For now I’m just fixing it with a simple check: get my client’s IP, compare it with the response and if they are the same, assume the CloudFlare API is down. I should probably implement more checks eventually…

#!/bin/sh
MY_IP=$(wget -O - -q http://myip.dnsomatic.com/)
NEW_IP=$(curl --silent https://api.cloudflare.com/client/v4/zones/<your-zone-id>/dns_records/<radius-server-hostname> \  
-H 'X-Auth-Email: <cloudflare-email>' \
-H 'X-Auth-Key: <cloudflare-key>' \
-H 'Accept: application/json' | grep -Eo '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}')
if [ "$NEW_IP" == "$MY_IP" ]; then
        logger "radius-update: CloudFlare API down"
        exit
fi
OLD_IP=`nvram get wl0_radius_ipaddr`

if [ "$NEW_IP" == "$OLD_IP" ]; then  
        logger "radius-update: IP unchanged"
        exit
fi

nvram set wl0_radius_ipaddr="$NEW_IP"  
nvram commit  
logger "radius-update: IP set to $NEW_IP, restarting wireless"  
/sbin/restart_wireless