Skip to content

Instantly share code, notes, and snippets.

@oomathias
Last active February 20, 2016 20:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save oomathias/5850554 to your computer and use it in GitHub Desktop.
Save oomathias/5850554 to your computer and use it in GitHub Desktop.
How to make your own DynDNS with CloudFlare.
#!/bin/sh
# Config
cfkey=89e495e7941cf9e40e6980d14a16bf023ccd4c91
cfuser=user@example.com
cfhost=example.com
cfname=local.example.com
cfid=123456789
# cfid = DNS Record ID. Available by using the rec_load_all call.
# curl -s https://www.cloudflare.com/api_json.html \
# -d "a=rec_load_all" \
# -d "tkn=$cfkey" \
# -d "email=$cfuser" \
# -d "z=$cfhost"
WAN_IP=`curl -s http://whatismyip.akamai.com`
if [ -f $HOME/.$cfname ]; then
OLD_WAN_IP=`cat $HOME/.$cfname`
else
echo "No file, need IP"
OLD_WAN_IP=""
fi
if [ "$WAN_IP" = "$OLD_WAN_IP" ]; then
echo "IP Unchanged"
else
echo $WAN_IP > $HOME/.$cfname
echo "Updating DNS to $WAN_IP"
curl -s https://www.cloudflare.com/api_json.html \
-d "a=rec_edit" \
-d "tkn=$cfkey" \
-d "email=$cfuser" \
-d "z=$cfhost" \
-d "id=$cfid" \
-d "type=A" \
-d "name=$cfname" \
-d "content=$WAN_IP" \
-d "service_mode=0" \
-d "ttl=120" > /dev/null
fi
@oomathias
Copy link
Author

To launch this script each time your network change on OSX, you need this launchd item in ~/Library/LaunchAgents/NetworkChanger.plist. Don't forget to update SCRIPT_PATH.

<?xml version="1.0" encoding="UTF-8"?>
<!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>fr.beugnon.networkchanger</string>
    <key>ProgramArguments</key>
    <array>
        <string>SCRIPT_PATH</string>
    </array>
    <key>WatchPaths</key>
    <array>
        <string>/Library/Preferences/SystemConfiguration</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>StartInterval</key>
    <integer>3600</integer>
</dict>
</plist>
  • Run every hour
  • Run at load
  • Run each time I suspect network change

Tell me if you know a better way ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment