Last active
August 7, 2016 00:54
-
-
Save charles-hollenbeck/7394512 to your computer and use it in GitHub Desktop.
Change all of the A records in your cloudflare account to point at a new IP
This file contains 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/env ruby | |
# Change all of the A records in your cloudflare account to point at a new IP | |
# chmod +x file.rb;./file.rb to run | |
# Example command: ./file.rb <ip> or let it prompt you | |
require 'cloudflare' # Laziness at it's best; Source: https://github.com/B4k3r/cloudflare | |
# Just some coloring crap | |
class String | |
def colorize(color_code); "\e[#{color_code}m#{self}\e[0m" end | |
def yellow; colorize(33) end | |
def red; colorize(31) end | |
def green; colorize(32) end | |
def cyan; colorize(36) end | |
end | |
key = "" # Cloudflare Client API Key | |
email = "" # Cloudflare Login Email | |
ip = ARGV.shift or nil # You can put an IP here, let it prompt you, or put it in the args ./script <ip> | |
ARGV.clear # Empty out ARGV so that gets doesn't freak | |
cf = CloudFlare.new(key, email) | |
until ip =~ /^([\d]{1,3}\.){3}([\d]{1,3})$/ do # Making sure the IP is an IP, prompts until it is | |
print "Enter new IP: ".yellow | |
ip = gets.to_s.chomp! | |
end | |
# This is where it requests all the domains in cloudflare and applys the new IP to them | |
domains_req = cf.zone_load_multi()["response"]["zones"]["objs"].each do |obj| | |
skipping = false; | |
domain = obj["zone_name"] | |
for o in cf.rec_load_all(domain)["response"]["recs"]["objs"] | |
if o["display_name"] == domain && o["type"] == "A" | |
if o["content"] == ip | |
puts "#{domain} is already correctly pointed!".cyan | |
skipping = true | |
else | |
id = o["rec_id"] | |
service_mode = o["service_mode"] | |
end | |
end | |
end | |
unless skipping | |
output = cf.rec_edit(domain, "A", id, domain, ip, 1, service_mode) | |
if output["result"] == "success" | |
puts "#{domain} is now pointed to #{ip} !".green | |
else | |
puts output["msg"].red | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment