Skip to content

Instantly share code, notes, and snippets.

@r4um
Created October 25, 2016 16:04
Show Gist options
  • Save r4um/30743f7c746c55a386bbd8873e55127b to your computer and use it in GitHub Desktop.
Save r4um/30743f7c746c55a386bbd8873e55127b to your computer and use it in GitHub Desktop.
Chek if an IP is in AWS IP Range
#!/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