获取IP地址的ASN编号(附带python代码)

ASN是为大型网络分配的全球唯一编号,查询IP地址隶属的ASN编号,可了解其网络运营商和大致位置。Nmap提供asn - query脚本用于查询,还会给出BGP、区域等信息,新手可多输出查看结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ASN(Autonomous System Number)是为每个大型网络分配的编号,该编号全球唯一。通过查询IP地址隶属的ASN编号,可以了解该IP地址隶属的网络运营商,以及大致的地址位置。Nmap提供asn-query脚本来查询IP地址的ASN编号,并给出BGP、区域、起始和节点编号等信息。
文件下载的话大伙网上找资源吧!我当时也是自己找的!大家可以在csdn上搜索,这些资源肯定都有。

def geoIp(ip):
    """
    获取ip详细信息方法
    """
    import pygeoip
    import geoip2.database
    gi = pygeoip.GeoIP('GeoLiteCity.dat')
    giv6 = pygeoip.GeoIP('GeoLiteCityv6.dat')
    geo_city = geoip2.database.Reader('GeoLite2-City.mmdb')
    geo_asn = geoip2.database.Reader('GeoLite2-ASN.mmdb')
    geo_isp = geoip2.database.Reader('GeoIP2-ISP.mmdb')

    response = geo_city.city(ip)
    response_asn = geo_asn.asn(ip)
    response_isp = geo_isp.isp(ip)
    rec_ipv6 = giv6.record_by_addr(ip)
    rec_ipv4 = gi.record_by_addr(ip)

    country_e_name = response.country.name  # 国家-英文名
    country_c_name = response.country.names.get('zh-CN')  # 国家-中文名
    city_e = response.subdivisions
    city_e_name = city_e[0].names.get('en')  # 城市-英文名
    city_c_name = city_e[0].names.get('zh-CN', '') # 城市-中文名
    continent_e = response.continent.code  # 洲、大陆(如亚洲、欧洲)-英文
    asn_code = response_asn.autonomous_system_number  # asn编码
    asn_cn = response_asn.autonomous_system_organization  # asn中文地址
    operator = response_isp.isp  # ip运营商英文名称



    city = rec_ipv6['city']  # 城市
    region = rec_ipv6['region_code']  #   地区
    country = rec_ipv6['country_name']  # 国家
    country_code = rec_ipv6['country_code']  # 国家代码
    area_code = rec_ipv6['area_code']  # 区域代码
    continent = rec_ipv6['continent']  # 洲、大陆(如亚洲、欧洲)
    long = rec_ipv6['longitude']  # 经度
    lat = rec_ipv6['latitude']  # 纬度

    city = rec_ipv4['city']  # 城市
    region = rec_ipv4['region_code']  # 地区
    country = rec_ipv4['country_name']  # 国家
    country_code = rec_ipv4['country_code']  # 国家代码
    area_code = rec_ipv4['area_code']  # 区域代码
    continent = rec_ipv4['continent']  # 洲、大陆(如亚洲、欧洲)
    long = rec_ipv4['longitude']  # 经度
    lat = rec_ipv4['latitude']  # 纬度

使用起来就像字典一样。新手可以多输出看下结果!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值