Skip to content

Instantly share code, notes, and snippets.

@reidblomquist
Created August 2, 2016 17:24
Show Gist options
  • Save reidblomquist/4afca0b501f629843572942bab08bb7d to your computer and use it in GitHub Desktop.
Save reidblomquist/4afca0b501f629843572942bab08bb7d to your computer and use it in GitHub Desktop.
Scrape aws' region json file to write out us-east-1 ip ranges for Ansible/iptables
require "json"
require "yaml"
require "net/http"
require "uri"
yaml_file = "#{Dir.pwd}/range.yml"
uri = URI.parse("https://ip-ranges.amazonaws.com/ip-ranges.json")
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Get.new(uri.request_uri)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
response = http.request(request)
if response.code == "200"
result = JSON.parse(response.body)
ranges = []
result["prefixes"].each do |doc|
if doc["region"] && doc["region"] == "us-east-1"
ranges << doc["ip_prefix"]
end
end
File.open(yaml_file, 'w') {|f| f.write ranges.to_yaml }
else
puts "ERROR!!!"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment