Skip to content

Instantly share code, notes, and snippets.

@foghina
Forked from riceo/cloudflare_dynamic_dns.py
Last active December 31, 2015 10:48
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 foghina/7975114 to your computer and use it in GitHub Desktop.
Save foghina/7975114 to your computer and use it in GitHub Desktop.
#!/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...)
#
# Updated for Python 3.
#
# @author Aaron Rice <aaron@duedil.com>
# @author Felix Oghină <felix@oghina.com>
import urllib.request
import urllib.parse
import json
import sys
import logging
try:
new_ip = urllib.request.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.request.urlopen("https://www.cloudflare.com/api_json.html", urllib.parse.urlencode(data).encode("utf-8")).readall().decode('utf-8'))
if dns_response[u'result'] == "success":
print("IP updated to " + new_ip.strip().decode('utf-8'))
else:
print("Error Setting IP")
except:
logging.exception("Error with cloudflare API")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment