Skip to content

Instantly share code, notes, and snippets.

@jefmathiot
Created April 18, 2018 09:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jefmathiot/51a19c180aec8ce51b72b28564f5d903 to your computer and use it in GitHub Desktop.
Save jefmathiot/51a19c180aec8ce51b72b28564f5d903 to your computer and use it in GitHub Desktop.
#!/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