使用Python脚本抓取aws所有的IP地址

为了满足客户业务需求,需要抓取AWS新加坡所有IP地址来设定路由。AWS官网有专门的链接列出所有IP地址,所以只需要利用脚本筛选出新加坡地址就OK。

所有IP地址链接:https://ip-ranges.amazonaws.com/ip-ranges.json

上述链接中“region”分别代表的意思:区域和可用区 - Amazon Elastic Compute Cloud

使用Python脚本抓取aws所有的IP地址_第1张图片

aws官网给出的抓取方法:AWS IP 地址范围 - AWS 一般参考

[root@PythonLearn home]# cat get_ips.py 
#!/usr/bin/env python
import requests

ip_ranges = requests.get('https://ip-ranges.amazonaws.com/ip-ranges.json').json()['prefixes']
amazon_ips = [item['ip_prefix'] for item in ip_ranges if item["region"] == "ap-southeast-1"]
ec2_ips = [item['ip_prefix'] for item in ip_ranges if item["service"] == "EC2"]


for ip in amazon_ips: print(str(ip))

区域选择: ap-southeast-1  (新加坡)

类型选择:ip_prefix   (只抓取ipv4)

[root@PythonLearn home]# ./get_ips.py 
52.95.212.0/22
52.93.8.0/22
103.246.148.0/23
52.219.132.0/22
52.92.56.0/22
52.93.19.236/32
15.221.8.0/21
54.240.199.0/24
54.240.227.0/24
52.93.19.237/32
52.95.35.0/24
52.144.231.64/26
99.82.173.0/24
52.94.11.0/24
52.219.40.0/22
52.219.32.0/21
52.219.48.0/22
150.222.78.0/24
13.248.107.0/24
52.94.198.96/28
52.219.124.0/22
52.219.128.0/22
150.222.108.0/24
54.240.226.0/24
52.119.184.0/22
43.250.193.0/24
43.250.192.0/24
52.144.224.128/26
54.239.0.96/28
52.93.63.0/24
203.83.220.0/22
54.255.254.192/26
54.251.31.128/26
52.219.132.0/22
52.92.56.0/22
52.219.40.0/22
52.219.32.0/21
52.219.48.0/22
52.219.124.0/22
52.219.128.0/22
52.94.11.0/24
13.228.69.0/24
52.220.191.0/26
52.221.221.128/29
99.82.173.0/24
13.248.107.0/24
13.251.113.64/26
13.251.116.0/23
18.138.134.128/25
18.138.244.0/23
13.250.186.128/27
13.250.186.160/27
3.0.5.32/29
52.76.127.0/24

IP地址会更新,所以脚本可以设置定期运行。

你可能感兴趣的:(运维,Python3,aws地址范围,python)