Created
April 16, 2012 21:52
-
-
Save riceo/2401865 to your computer and use it in GitHub Desktop.
Dynamic DNS with Cloudflare - Python Script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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" |
Hey! I made a script similar to this, but it can update several hosts (like example.com and www.example.com).
Check it out here: https://github.com/TTSDA/CfDDNS
@ttsda nice!
seems like parameters are changed now...
I'd imagine so!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
Sorry I haven't responded sooner - I had no idea you commented.
This was a quick hack for dynamic DNS via Cloudflare. If you run it as a cronjob on your server it will use the Cloudflare API to update your server's current public IP adress for the specified DNS entry on Cloudflare.
The change should only take a few minutes max to propagate.
I wouldn't trust it in a production environment, but it should work fine for testing / dev.
Let me know if you have any problems, and feel free to email me if I don't respond quick enough here!
Cheers,
Aaron