使用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 篇原創文章 · 獲贊 20 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章