環境
- Python 3
やりたいこと
AS番号を渡したらIP帯域をCIDR形式で全部取得したい。
コード
import requests import json def get_ipv4_ranges(as_number): url = f"https://ipinfo.io/widget/demo/{as_number}?dataset=asn" response = requests.get(url) data = json.loads(response.text) ipv4_ranges = [] for prefix in data["prefixes"]: if "/" in prefix["netblock"]: ipv4_ranges.append(prefix["netblock"]) return ipv4_ranges def save_to_file(as_number, ipv4_ranges): with open(f"{as_number}.txt", "w") as file: for range in ipv4_ranges: file.write(f"{range}\n") as_number = "AS7684" ipv4_ranges = get_ipv4_ranges(as_number) save_to_file(as_number, ipv4_ranges)