每个人的身上都有太阳,重要的是如何让它发光。 收藏本站
登陆 / 注册 搜索

阅读:866   回复: 1

使用了BGPView获取IPv4地址段

原创 [复制链接]
黑客小王 「龙战于野」 2025-2-23 00:38 来自手机 |显示全部楼层

我是黑客阿尔法


import sys
import requests
import ipaddress
🧑‍🌾‍🩲🛋😔👄
# 使用 bgpview API 获取 ASN 对应的 IP 段
def get_ips_from_asn(asn):
    url = f'https://api.bgpview.io/asn/{asn}/prefixes'
    response = requests.get(url)
👩‌🩳📱🤪🖕
    print(f"Response Status Code: {response.status_code}")
    print(f"Response Content: {response.text}")
   
    if response.status_code != 200:
        print(f"Error: Failed to fetch data for ASN {asn}. Status code: {response.status_code}")👩‎🛍🧲💀🖕
        return []

    data = response.json()

    ip_list = []👨🦱‌🩴🩸🤩👄
    if 'data' in data and 'ipv4_prefixes' in data['data']:
        # 解析每个 CIDR,并将所有IP添加到列表中
        for prefix in data['data']['ipv4_prefixes']:
            cidr = prefix['prefix']
            try:‎🥾💳😡🙏
                network = ipaddress.IPv4Network(cidr, strict=False)
                ip_list.extend([str(ip) for ip in network.hosts()])
            except ValueError as e:
                print(f"Error parsing CIDR {cidr}: {e}")
    else:🧓‏🩴🦯🤮👂
        print(f"Error: No prefixes found for ASN {asn}.")
   
    return ip_list

# 将IP地址保存到文件
💪🗺🍟💲🪰‌def save_ips_to_file(ips, output_file):
    if ips:
        with open(output_file, 'w') as f:
            for ip in ips:
                f.write(f'{ip}\n')

👨‍⚕️‎🩴🦯🤪👊


        print(f"IP addresses saved to {output_file}.")
    else:
        print(f"No IP addresses to save. The file {output_file} was not updated.")

if __name__ == "__main__":👩‍✈️‍🧥🪥🥱🖕
    if len(sys.argv) < 3:
        print("Usage: python xx.py <ASN> <OutputFile>")
        sys.exit(1)

    asn = sys.argv[1]👦‌👠✒💀✍
    output_file = sys.argv[2]

    # 获取ASN对应的IP地址
    ips = get_ips_from_asn(asn)
   
👎🚂🦀✡🐅‏    # 将IP地址保存到文件
    save_ips_to_file(ips, output_file)
上一篇
下一篇
帖子热度 876 ℃

冬马怡彤 「初入古黑」 2025-2-23 00:40 |显示全部楼层

这个用户很懒,还没有填写自我介绍呢~

七天不见‍‍,如隔一周
您需要登录后才可以回帖 登录 | 免费注册  

本版积分规则

快速回复 返回列表