Skip to content

Instantly share code, notes, and snippets.

@alfo
Created January 14, 2014 17:48
Show Gist options
  • Save alfo/8422533 to your computer and use it in GitHub Desktop.
Save alfo/8422533 to your computer and use it in GitHub Desktop.
Cron job to update Cloudflare's DNS to point to your current IP address. Useful for people with dynamic public IPs because their stupid ISP won't let them have a static one.
<?php
// Grab the IP address
// This is the fastest method I've found so far
$ip = system('dig +short myip.opendns.com @resolver1.opendns.com');
// For cron logs
print "New IP is: " . $ip;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.cloudflare.com/api_json.html");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
$data = array(
'a' => 'DIUP',
'tkn' => 'put an api token in here please',
'email' => 'also an email in here',
'ip' => $ip,
'hosts' => 'comma seperated domains pls'
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
echo $output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
?>
@acollien
Copy link

acollien commented Oct 31, 2021

So 8 years later, I (superNewb) and trying to achieve this same thing. I am guessing (hoping) that in 8 years, it may be a little easier to achieve? Like call using personal API, or something?

E.g. on my googleDomains DNS, I could just add the following cron job to my DDWRT router, and it'd run every 15 mins to check for an IP update. I am hoping there is something as - if not more - simple for cloudflare now?

0,15,30,45 * * * * root /usr/bin/curl -4 --user-agent curl -s -get https://MY_USER:MY_PASS@domains.google.com/nic/update?hostname=MY_DNS | /usr/bin/logger -s -t dns-updater

NB: GoogleDomains don't give API, so they generate random user/pass credentials for your DNS (i.e. 'MY_USER_MY_PASS' above.

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