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

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

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

上述链接中“region”分别代表的意思:https://docs.aws.amazon.com/zh_cn/AWSEC2/latest/UserGuide/using-regions-availability-zones.html

aws官网给出的抓取方法:https://docs.aws.amazon.com/zh_cn/general/latest/gr/aws-ip-ranges.html

[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
发布了22 篇原创文章 · 获赞 21 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_41565459/article/details/103416735