Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/bin/env ruby
require 'net/http'
require 'json'
addresses = Array.new(33) { |i| 2**(32 - i) }
uri = URI('https://ip-ranges.amazonaws.com/ip-ranges.json')
req = Net::HTTP::Get.new(uri)
response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
http.request(req)
end
total = JSON.parse(response.body)['prefixes'].reduce(0) do |count, prefix|
range = prefix['ip_prefix'].split('/')
range_count = addresses[range.last.to_i]
puts "Range #{prefix['ip_prefix']}, #{range_count}"
count + range_count
end
puts "Total #{total}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment