Skip to content

Instantly share code, notes, and snippets.

@knight42
Created August 30, 2018 10:29
Show Gist options
  • Select an option

  • Save knight42/c4ab62d5342a5b86e126b4e6190900ad to your computer and use it in GitHub Desktop.

Select an option

Save knight42/c4ab62d5342a5b86e126b4e6190900ad to your computer and use it in GitHub Desktop.
get aws ip ranges in cn-north-1 region
#!/usr/bin/env python -O
# -*- coding: utf-8 -*-
#from __future__ import print_function, unicode_literals, with_statement, division, absolute_import
# import os
# import random
# import requests
import sys
import requests
from netaddr import IPSet
# https://docs.aws.amazon.com/general/latest/gr/aws-ip-ranges.html
def main():
# ipv4
# ipv6_prefixes for ipv6 ranges
ip_ranges = requests.get(
'https://ip-ranges.amazonaws.com/ip-ranges.json').json()['prefixes']
with open('aws_cn-north-1_ip_ranges.txt', 'w') as fout:
s = IPSet(
map(lambda x: x['ip_prefix'],
filter(lambda x: x['region'] == 'cn-north-1', ip_ranges)))
for i in s.iter_cidrs():
print(i, file=fout)
return 0
if __name__ == '__main__':
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment