Created
April 18, 2018 09:29
-
-
Save jefmathiot/51a19c180aec8ce51b72b28564f5d903 to your computer and use it in GitHub Desktop.
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 | |
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