Skip to content

Instantly share code, notes, and snippets.

@riceo
Created April 16, 2012 21:52
Show Gist options
  • Select an option

  • Save riceo/2401865 to your computer and use it in GitHub Desktop.

Select an option

Save riceo/2401865 to your computer and use it in GitHub Desktop.
Dynamic DNS with Cloudflare - Python Script
#!/usr/bin/python
# Set up a new A record in Cloudflare, add the details of it along with your account details below
# Make sure this script runs on startup (or whenever you get a new IP...)
#
# @author Aaron Rice <aaron@duedil.com>
import urllib
import json
import sys
try:
new_ip = urllib.urlopen("http://my-ip.heroku.com/").read()
except:
print "Error getting IP"
sys.exit()
# Put your Cloudflare settings here.
# The host must be an A record that already exists in cloudflare
data = {
"a" : "DIUP",
"tkn" : "API_TOKEN_HERE",
"u" : "CLOUDFLARE_EMAIL_HERE",
"ip" : new_ip.strip(),
"z" : "DOMAIN_HERE",
"hosts" : "DNSNAME.DOMAIN_HERE",
}
try:
dns_response = json.loads(urllib.urlopen("https://www.cloudflare.com/api_json.html", urllib.urlencode(data)).read())
if dns_response[u'result'] == "success":
print "IP updated to " + new_ip,
else:
print "Error Setting IP"
except:
print "Error with cloudflare API"
@riceo
Copy link
Copy Markdown
Author

riceo commented Apr 12, 2014

@ttsda nice!

@nardev
Copy link
Copy Markdown

nardev commented Jan 9, 2017

seems like parameters are changed now...

@riceo
Copy link
Copy Markdown
Author

riceo commented Nov 20, 2018

I'd imagine so!

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