Skip to content

Instantly share code, notes, and snippets.

@kiliankoe
Last active February 22, 2019 20:37
Show Gist options
  • Save kiliankoe/268c13efa87c510bf8ad to your computer and use it in GitHub Desktop.
Save kiliankoe/268c13efa87c510bf8ad to your computer and use it in GitHub Desktop.
Cloudflare DynDNS
#! /usr/bin/env ruby
require 'net/http'
require 'cloudflare'
# Email used for login
CLOUDFLARE_EMAIL = ENV["CLOUDFLARE_EMAIL"]
# API Key, can be found here => https://www.cloudflare.com/a/account/my-account
CLOUDFLARE_API_KEY = ENV["CLOUDFLARE_API_KEY"]
# The zone (aka domain) this is for, e.g. 'example.com'
CLOUDFLARE_ZONE = ENV["CLOUDFLARE_ZONE"]
# The specific record name to edit, e.g. 'record.example.com'
CLOUDFLARE_RECORD_NAME = ENV["CLOUDFLARE_RECORD_NAME"]
def get_ip
Net::HTTP.get(URI.parse('http://canihazip.com/s'))
end
def update_cloudflare(ip)
cf = CloudFlare::connection(CLOUDFLARE_API_KEY, CLOUDFLARE_EMAIL)
begin
rec_id = ''
records = cf.rec_load_all(CLOUDFLARE_ZONE)
records['response']['recs']['objs'].each do |record|
if record['name'] == CLOUDFLARE_RECORD_NAME
rec_id = record['rec_id']
break
end
end
# Sending '1' for the TTL = 'automatic' meaning approx. 300 (5 minutes)
cf.rec_edit(CLOUDFLARE_ZONE, 'A', rec_id, CLOUDFLARE_RECORD_NAME, ip, 1)
rescue => e
puts e.message
else
puts "Successfully updated A record for #{CLOUDFLARE_RECORD_NAME} to #{ip}"
end
end
update_cloudflare(get_ip)
@kiliankoe
Copy link
Author

kiliankoe commented Mar 7, 2016

See the blog post for context: https://blog.kilian.io/cheap-dyndns-alternative/

@hoodie
Copy link

hoodie commented Mar 8, 2016

why only a gist?

@kiliankoe
Copy link
Author

a dedicated repo feels overkill for a single file. an reason a gist is no good?

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