python module for nslookup

后端 未结 9 825
渐次进展
渐次进展 2020-12-10 00:27

Is there a python-module that\'s doing the same stuff as nslookup does? I am planning to use nslookup on digging some information regarding the domain of a URL to be scrappe

相关标签:
9条回答
  • 2020-12-10 01:09
    $ sudo apt install python-dns
    $ python
    >>> import DNS
    >>> d = DNS.DnsRequest(server="1.1.1.1", timeout=1)                                       
    >>> r = d.req(name="stackoverflow.com", qtype="A")
    >>> [i['data'] for i in r.answers]
    ['151.101.129.69', '151.101.193.69', '151.101.1.69', '151.101.65.69']
    

    Further details like ttl can also be accessed, plus this approach has the advantage of letting you specify which nameserver to use, instead of just using your system's default.

    0 讨论(0)
  • 2020-12-10 01:15

    I'm using the following code:

    import socket
    
    ip_list = []
    ais = socket.getaddrinfo("www.yahoo.com",0,0,0,0)
    for result in ais:
      ip_list.append(result[-1][0])
    ip_list = list(set(ip_list))
    

    Or using a comprehension as:

    ip_list = list({addr[-1][0] for addr in socket.getaddrinfo(name, 0, 0, 0, 0)})
    
    0 讨论(0)
  • 2020-12-10 01:15

    Previous answers are correct but here is what I would use socket, it "provides access to the BSD socket interface. It is available on all modern Unix systems, Windows, MacOS, and probably additional platforms."

    import socket
    
    distinct_ips = []
    # 0,0,0,0  is for (family, type, proto, canonname, sockaddr)
    #change some_site.com to whatever your are looking up of course
    socket_info = socket.getaddrinfo("some_site.com",0,0,0,0)
    for result in socket_info:
        ns_ip = result[4][0]
        if distinct_ips.count(ns_ip)==0:
            distinct_ips.append(ns_ip)
            print(ns_ip)
    
    0 讨论(0)
  • 2020-12-10 01:22

    I needed to track down A records in AWS Route 53 using CNames. AKA messaging.myCompany.com to moreSpecificMessaging.myCompanyInternal.com

    I also use Socket, but another rather hidden method.

    import socket
    
    addr1 = socket.gethostbyname_ex('google.com')
    
    print(addr1)
    

    https://docs.python.org/3/library/socket.html#socket.gethostbyname_ex

    0 讨论(0)
  • 2020-12-10 01:24

    the problem is that socket.gethostbyname() returns only one ip-address. nslookup returns as many as it has. I use:

    import subprocess
    
    process = subprocess.Popen(["nslookup", "www.google.com"], stdout=subprocess.PIPE)
    output = process.communicate()[0].split('\n')
    
    ip_arr = []
    for data in output:
        if 'Address' in data:
            ip_arr.append(data.replace('Address: ',''))
    ip_arr.pop(0)
    
    print ip_arr
    

    it will print:

    ['54.230.228.101', '54.230.228.6', '54.230.228.37', '54.230.228.80', '54.230.228.41', '54.230.228.114', '54.230.228.54', '54.230.228.23']
    
    0 讨论(0)
  • 2020-12-10 01:24

    I'm using the following code:

    import socket
    addr1 = socket.gethostbyname('google.com')
    addr2 = socket.gethostbyname('yahoo.com')
    print(addr1, addr2)
    
    0 讨论(0)
提交回复
热议问题