从文本文件中查找 IP,生成另一个具有特定格式的文本文件

从文本文件中查找 IP,生成另一个具有特定格式的文本文件

我想要一种查找文本文件中的所有域(每行一个域)并生成另一个文本文件的方法,输出为 IP 地址、空格、域名、空格,然后是域名万维网。前置它。

例如,如果源文本文件包含两行:

1.gravatar.com
abcya.com

新的文本文件将包含 3 行,因为 1.gravatar.com 同时具有 IPv4 和 IPv6 地址:

72.21.91.121 1.gravatar.com www.1.gravatar.com
2a04:fa87:fffe::c000:4902 1.gravatar.com www.1.gravatar.com
104.198.14.52 abcya.com www.abcya.com

我正在运行 Ubuntu 衍生版本,可以使用 nslookup 获取 IPv4 和 IPv6 地址。然而,源文本文件是包含 2,000 多个域的列表 - 因此手动执行此操作将花费很长时间,并且很容易出错。

如果答案也允许没有 IP 地址。如果域不再存在(如alwaysbeready.mybigcommerce.com 的情况),nslookup 将返回 ** server can't find alwaysbeready.mybigcommerce.com: NXDOMAIN 因此,可能用 NXDOMAIN 代替结果文本中的 IP 地址文件?

预先感谢任何可以提供帮助的人。

答案1

一个Python解决方案

#!/usr/bin/python3

import socket 


#this module is core networking module in Python, 
#can be used to resolve domain names.

sourcefile = 'sourcefile.txt' #file with domain names
outfile = 'results.txt' #file to write the IP addresses

with open(sourcefile, 'r') as inputf: 
    #This opens the sourcefile in read mode to see what are the domains


    with open(outfile, 'a') as outputf: 
        #This opens the outfile in append mode to write the results


        domains = inputf.readlines() 
        #This reads all the domains in sourcefile line by line


        for domain in domains: 
            #This for loop will go one by one on domains.


            domain = domain.strip("\n") 
                #as the every domain in the file are in newline,
                #the socket function will have trouble, so strip off the newline char


            try:
                resolution = (socket.getaddrinfo(domain, port=80,type=2))
                for ip in resolution:
                    outputf.write(str(ip[4][0])+" "+domain+ " www."+domain+"\n" )
            except:
                outputf.write("Could not resolve "+domain+" www."+domain+"\n")
                #getaddinfo("domain") gets all the IP addresses. 

输入 :

1.gravatar.com
abcya.com
allaboutbirds.org
google.com
akamai.de

输出 :

192.0.73.2 1.gravatar.com www.1.gravatar.com
2a04:fa87:fffe::c000:4902 1.gravatar.com www.1.gravatar.com
104.198.14.52 abcya.com www.abcya.com
128.84.12.109 allaboutbirds.org www.allaboutbirds.org
216.58.197.78 google.com www.google.com
2404:6800:4007:810::200e google.com www.google.com
104.127.218.235 akamai.de www.akamai.de
2600:140b:a000:28e::35eb akamai.de www.akamai.de
2600:140b:a000:280::35eb akamai.de www.akamai.de

答案2

bash解析以下输出的hacky解决方案nslookup

#!/bin/bash

get_lines() {
        ips=($(nslookup -type=A "$1" | grep -Po -m1 "Address: \K.*"))
        ips+=($(nslookup -type=AAAA "$1" | grep -Po -m1 "has AAAA address \K.*"))

        if [ ${#ips[@]} -ne 0 ]; then
                printf "%s $1 www.$1\n" "${ips[@]}"
        else
                printf 'NXDOMAIN %s www.%s\n' "$1" "$1"
        fi
}

while read domain; do
        if [ -z "$domain" ] || [ "${domain:0:1}" = "#" ]; then
                # skip empty line and line starting with '#'
                continue
        fi
        get_lines "$domain"
done < "$1"

解释:

  1. 对于 IPv4,我们grep为之后的 IP 地址Address:

    例子:

    $ nslookup -type=A 1.gravatar.com
    Server:         8.8.8.8
    Address:        8.8.8.8#53
    
    Non-authoritative answer:
    Name:   1.gravatar.com
    Address: 192.0.73.2
    
  2. 对于 IPv6 地址,我们grep为之后的 IP 地址has AAAA address

    例子:

    $ nslookup -type=AAAA 1.gravatar.com
    Server:         8.8.8.8
    Address:        8.8.8.8#53
    
    Non-authoritative answer:
    1.gravatar.com  has AAAA address 2a04:fa87:fffe::c000:4902
    
    Authoritative answers can be found from:
    
  3. 如果 IPv4 和 IPv6 均失败,则输出为NXDOMAIN domain www.domain

  4. #输入文件中的空行或以 开头的行将被跳过。

输出:

我的测试域文件如下所示:

$ cat domains.txt
1.gravatar.com
abcya.com
alwaysbeready.mybigcommerce.com
# this is a comment followed by a newline

allaboutbirds.org
aliceinwonderland.ca
allcancode.com

测试运行:

$ ./getips.sh domains.txt
192.0.73.2 1.gravatar.com www.1.gravatar.com
2a04:fa87:fffe::c000:4902 1.gravatar.com www.1.gravatar.com
104.198.14.52 abcya.com www.abcya.com
NXDOMAIN alwaysbeready.mybigcommerce.com www.alwaysbeready.mybigcommerce.com
128.84.12.109 allaboutbirds.org www.allaboutbirds.org
198.168.252.18 aliceinwonderland.ca www.aliceinwonderland.ca
216.239.32.21 allcancode.com www.allcancode.com
2001:4860:4802:32::15 allcancode.com www.allcancode.com

您可以将输出重定向到带有./getips.sh domains.txt > results.txt.

相关内容