Skip to content

Instantly share code, notes, and snippets.

@creaktive
Created March 5, 2024 13:14
Show Gist options
  • Save creaktive/cc4b63622ae3c87f5edd81486d354b7f to your computer and use it in GitHub Desktop.
Save creaktive/cc4b63622ae3c87f5edd81486d354b7f to your computer and use it in GitHub Desktop.
AWS IP address ranges scanner
#!/usr/bin/env perl
use strict;
use warnings qw(all);
use constant IP_RANGES_JSON => 'https://ip-ranges.amazonaws.com/ip-ranges.json';
use JSON::PP qw();
use LWP::Simple qw();
use NetAddr::IP qw();
my $needle = NetAddr::IP->new($ARGV[0] || '127.0.0.1');
my $json = JSON::PP->new
->ascii
->canonical
->pretty
;
my $filename = IP_RANGES_JSON;
$filename =~ s{^.*/}{};
my $status = LWP::Simple::mirror(IP_RANGES_JSON, $filename);
die "HTTP status $status when downloading @{[ IP_RANGES_JSON ]}\n"
if $status ne 200 && $status ne 304;
my $ip_ranges = do {
local $/;
open(my $fh, '<', $filename) || die "Can't read $filename: $@\n";
$json->decode(<$fh>);
};
for my $prefix ($ip_ranges->{prefixes}->@*) {
my $haystack = NetAddr::IP->new($prefix->{ip_prefix});
if ($needle->within($haystack)) {
print $json->encode($prefix);
last;
}
}
exit 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment