Skip to content

Instantly share code, notes, and snippets.

@thash
Created April 28, 2016 05:56
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 thash/e1ff9539b75f9c045cab34a12a38a9ed to your computer and use it in GitHub Desktop.
Save thash/e1ff9539b75f9c045cab34a12a38a9ed to your computer and use it in GitHub Desktop.
require 'ipaddr'
ec2_ranges = %w_
103.4.8.0/21
175.41.192.0/18
176.32.64.0/19
176.34.0.0/19
176.34.32.0/19
46.51.224.0/19
52.192.0.0/15
52.196.0.0/14
52.68.0.0/15
52.95.243.0/24
52.95.255.48/28
54.150.0.0/16
54.168.0.0/16
54.178.0.0/16
54.199.0.0/16
54.238.0.0/16
54.248.0.0/15
54.250.0.0/16
54.64.0.0/15
54.92.0.0/17
54.95.0.0/16
_.map{|range| IPAddr.new(range) }
# $ dig +short lambda.ap-northeast-1.amazonaws.com a
# 52.69.6.111
# 52.69.23.0
# 52.69.25.229
# 52.69.54.22
# 52.69.67.28
# 52.69.67.226
# 54.64.249.58
# 54.249.77.229
lambda_ips = %w_
52.69.6.111
52.69.23.0
52.69.25.229
52.69.54.22
52.69.67.28
52.69.67.226
54.64.249.58
54.249.77.229
_.map{|l| IPAddr.new(l) }
lambda_ips.each do |l|
catch(:found) do
ec2_ranges.each_with_index do |range, i|
if range.include?(l)
prefixlen = 32 - (2 ** 32 - 1 - range.instance_eval{ @mask_addr }).to_s(2).length
puts "#{l} is included in #{range.to_s}/#{prefixlen}"
throw(:found)
end
end
puts "#{l} isn't included in any of ip ranges"
end
end
# 52.69.6.111 is included in 52.68.0.0/15
# 52.69.23.0 is included in 52.68.0.0/15
# 52.69.25.229 is included in 52.68.0.0/15
# 52.69.54.22 is included in 52.68.0.0/15
# 52.69.67.28 is included in 52.68.0.0/15
# 52.69.67.226 is included in 52.68.0.0/15
# 54.64.249.58 is included in 54.64.0.0/15
# 54.249.77.229 is included in 54.248.0.0/15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment