domaintoip批量获取域名ip(bat,python,exe脚本)

本文介绍了三种方法来批量获取子域名对应的IP地址,包括使用BAT脚本、Python脚本以及DNSX工具。Python脚本利用dnspython库进行域名解析,DNSX适用于大量域名但可能有漏报。此外,还提供了使用GeoIP库过滤中国境内或湖南省内的IP地址的Python示例。
摘要由CSDN通过智能技术生成

1. 介绍

需要批量获取子域名的对应ip地址。

2. 使用场景

bat的适用于少量域名资产(基本无漏报)。

python的适用于多量域名资产(基本无漏报)。

dnsx的适用于大量域名资产(会有漏报)。

3. 代码

bat代码:子域名放入url.txt里面,结果为toip.txt

@ECHO OFF
FOR /f "delims=" %%i in (url.txt) do (
FOR /f "tokens=2 delims=[]" %%a in ('ping -n 1 %%i -4' ) do (
ECHO %%i %%a >>"toIP.txt"
))
PAUSE

建议:运行第一次之后,把结果保存为toip1.txt,再次运行一次,然后使用精英txt文本整理3.4  提取共同部分,能过滤掉部分多个域名解析的ip,扫了cdn也没用,还耽误时间。

用法:另存代码为:批量获取域名ip.bat 双击运行

python代码:子域名放入url.txt里面,结果为result.txt



#!/usr/bin/env python
#coding:utf-8


from socket import gethostbyname
DOMAIN= "url.txt"

with open(DOMAIN,'r') as f:

     for line in f.readlines():
        try:
            host = gethostbyname(line.strip('\n'))  #域名反解析得到的IP
        except Exception as e:
            with open('error.txt','a+') as ERR:  #error.txt为没有IP绑定的域名
                ERR.write(line.strip()+ '\n')
        else:
            with open('result.txt','a+') as r: #result.txt里面存储的是批量解析后的结果
                r.write(line.strip('\n') + ' ')   #显示有ip绑定的域名,用空格隔开
                r.write(host + '\n')

 

 

建议:同上

用法:另存代码为:domaintoip.py 

python37或python38  domaintoip.py

dnsx.exe

下载地址:https://github.com/projectdiscovery/dnsx

用法:dnsx -l url.txt -resp -a >> all.txt

引申:

python38 -m pip install geoip2  
python38 -m pip install python-geoip-geolite2
python38 -m pip install maxminddb-geolite2

mmdb下载地址:https://github.com/P3TERX/GeoLite.mmdb/releases/

python3  domaintoip_cn.py
python3  domaintoip_hn.py

domaintoip_china(直接解析域名对应的ip)只要中国范围内的

import concurrent.futures  
from dns import resolver  
import ipaddress  
from geoip2.database import Reader  
  
# 初始化GeoIP2数据库读取器  
geoip_reader = Reader('GeoLite2-Country.mmdb')  # 替换为你的GeoLite2-Country.mmdb文件路径  
  
# 检查IP地址是否属于中国大陆  
def is_ip_in_china(ip):  
    try:  
        response = geoip_reader.country(ip)  
        return response.country.iso_code == 'CN'  
    except Exception as e:  
        print(f"Error checking IP {ip}: {e}")  
        return False  
  
# 解析域名并获取IP地址  
def resolve_domain(domain):  
    try:  
        # 使用dnspython解析域名  
        answers = resolver.resolve(domain, 'A')  
        for rdata in answers:  
            ip = rdata.address  
            # 检查IP地址是否属于中国大陆  
            if is_ip_in_china(ip):  
                return domain, ip  
    except (resolver.NXDOMAIN, resolver.NoAnswer, resolver.Timeout, Exception) as e:  
        # 域名不存在、没有答案、超时或其他异常  
        print(f"Error resolving {domain}: {e}")  
    return None, None  
  
# 主处理函数  
def process_domains(filename):  
    with open(filename, 'r', encoding='utf-8') as file:  
        domains = file.read().splitlines()  
  
    with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor:  
        futures = {executor.submit(resolve_domain, domain): domain for domain in domains}  
        for future in concurrent.futures.as_completed(futures):  
            domain = futures[future]  
            domain_result, ip_result = future.result()  
            if domain_result and ip_result:  
                with open('china.txt', 'a', encoding='utf-8') as f:  
                    f.write(f"{domain}\t{ip_result}\n")  
  
# 主程序入口  
if __name__ == "__main__":  
    domain_file = 'url.txt'  # 域名列表文件  
    process_domains(domain_file)  
    print("Processing completed.")

domaintoip_hunan(直接解析域名对应的ip)只要湖南省范围内的

import concurrent.futures  
import socket  
from dns import resolver  
from geoip2.database import Reader  
  
# 初始化GeoIP2数据库读取器  
geoip_reader = Reader('GeoLite2-City.mmdb')  # 替换为你的GeoLite2-City.mmdb文件路径  
  
# 解析域名并获取IP地址  
def resolve_domain(domain):  
    try:  
        # 使用dnspython解析域名  
        answers = resolver.resolve(domain, 'A')  
        for rdata in answers:  
            ip = rdata.address  
            # 检查IP地址是否位于中国湖南省  
            response = geoip_reader.city(ip)  
            if response.country.iso_code == 'CN' and 'Hunan' in [sub.name for sub in response.subdivisions]:  
                return domain, ip  
    except (resolver.NXDOMAIN, resolver.NoAnswer, resolver.Timeout, socket.timeout, socket.gaierror):  
        # 域名不存在、没有答案、超时或其他socket错误  
        pass  
    except Exception as e:  
        print(f"Error resolving {domain}: {e}")  
    return None, None  
  
# 主处理函数  
def process_domains(filename):  
    with open(filename, 'r', encoding='utf-8') as file:  
        domains = file.read().splitlines()  
  
    with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:  
        futures = {executor.submit(resolve_domain, domain): domain for domain in domains}  
        for future in concurrent.futures.as_completed(futures):  
            domain = futures[future]  
            domain_result, ip_result = future.result()  
            if domain_result and ip_result:  
                with open('hunan.txt', 'a', encoding='utf-8') as f:  
                    f.write(f"{domain_result}\t{ip_result}\n")  
  
# 主程序入口  
if __name__ == "__main__":  
    domain_file = 'url.txt'  # 域名列表文件  
    process_domains(domain_file)  
    print("Processing completed.")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值