Created
October 25, 2016 16:04
-
-
Save r4um/30743f7c746c55a386bbd8873e55127b to your computer and use it in GitHub Desktop.
Chek if an IP is in AWS IP Range
This file contains hidden or 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 'ipaddr' | |
require 'json' | |
url = 'https://ip-ranges.amazonaws.com/ip-ranges.json' | |
uri = URI(url) | |
response = Net::HTTP.get(uri) | |
aws_ranges = JSON.parse(response) | |
aws_ranges = aws_ranges['prefixes'] | |
aws_ranges.each do |aws_range| | |
aws_range['ipaddr_net'] = IPAddr.new(aws_range['ip_prefix']) | |
end | |
ARGV.each do |arg| | |
in_ranges = aws_ranges.select { |range| range['ipaddr_net'].include? arg } | |
puts "IP #{arg} in ranges" | |
puts in_ranges | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment