Skip to content

Instantly share code, notes, and snippets.

@stevelacey
Last active September 3, 2020 05:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stevelacey/bd6e6e0f429e54a3633250071567a69a to your computer and use it in GitHub Desktop.
Save stevelacey/bd6e6e0f429e54a3633250071567a69a to your computer and use it in GitHub Desktop.
Sync local IP to Cloudflare DNS on MacOS network change, lets you make your own xip.io but with a static URL
#!/bin/bash
CLOUDFLARE_API_KEY=changeme
CLOUDFLARE_DNS_RECORD=*.somewildcard.yourdomain.com
CLOUDFLARE_DNS_RECORD_ID=
CLOUDFLARE_ZONE=yourdomain.com
CLOUDFLARE_ZONE_ID=
if [[ "${CLOUDFLARE_ZONE_ID}" == "" ]]; then
CLOUDFLARE_ZONES=`curl -s "https://api.cloudflare.com/client/v4/zones" \
-H "Authorization: Bearer ${CLOUDFLARE_API_KEY}" \
-H "Content-Type: application/json"`
CLOUDFLARE_ZONE_ID=`echo ${CLOUDFLARE_ZONES} | jq -r ".result[] | select(.name == \"${CLOUDFLARE_ZONE}\").id"`
fi
if [[ "${CLOUDFLARE_DNS_RECORD_ID}" == "" ]]; then
CLOUDFLARE_DNS_RECORDS=`curl -s "https://api.cloudflare.com/client/v4/zones/${CLOUDFLARE_ZONE_ID}/dns_records" \
-H "Authorization: Bearer ${CLOUDFLARE_API_KEY}" \
-H "Content-Type: application/json"`
CLOUDFLARE_DNS_RECORD_ID=`echo ${CLOUDFLARE_DNS_RECORDS} | jq -r ".result[] | select(.name == \"${CLOUDFLARE_DNS_RECORD}\").id"`
fi
IP=$(ifconfig | grep -A 2 "en" | grep broadcast | cut -d " " -f 2 | head -n 1)
curl -s -X PATCH "https://api.cloudflare.com/client/v4/zones/${CLOUDFLARE_ZONE_ID}/dns_records/${CLOUDFLARE_DNS_RECORD_ID}" \
-H "Authorization: Bearer ${CLOUDFLARE_API_KEY}" \
-H "Content-Type: application/json" \
-d "{\"content\": \"${IP}\"}"
<?xml version="1.0" encoding="UTF-8"?>
<!-- launchctl load ~/Library/LaunchAgents/steve.networkchange.plist -->
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" \
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>networkchange</string>
<key>LowPriorityIO</key>
<true/>
<key>ProgramArguments</key>
<array>
<string>/Users/steve/Sites/scripts/networkchange.sh</string>
</array>
<key>WatchPaths</key>
<array>
<string>/etc/resolv.conf</string>
<string>/var/run/resolv.conf</string>
<string>/private/var/run/resolv.conf</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment