相关文章推荐
不拘小节的电影票  ·  PowerBi => ...·  1 年前    · 
憨厚的篮球  ·  Visual Studio Code ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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 scrapped. I know I can use os.sys to call nslookup but I am just wondering if there is a python-module for this already. Thanks in advance!

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)})
                Socket doesn't appear to work very well for nslookup, especially if there are multiple domains for all the hostnames.
– Nathan McKaskle
                May 10, 2019 at 15:14
                dnspython is a utility to work with DNS, /etc/hosts is thus not used. For simple forward DNS lookups, it's better to use socket.gethostbyname() (fount  on github of dnspython)
– Rafal
                Jun 13, 2020 at 7:18

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']

Note that socket.getfqdn() can return the full-qualified name of a hostname. See: http://docs.python.org/2/library/socket.html?highlight=socket.getaddrinfo#socket.getfqdn

For example:

python -c  'import socket; print(socket.gethostname()); print(socket.getfqdn());'
myserver
myserver.mydomain.local

But the result depends the /etc/hosts configuration. If you have:

$ cat /etc/hosts
127.0.0.1       myserver  localhost.localdomain  localhost

The result of socket.getfqdn() will be:

python -c  'import socket; print(socket.getfqdn());'
localhost.localdomain

Oooops! To solve that, the only solution I know is to change the /etc/hosts as follow:

$ cat /etc/hosts
127.0.0.1       myserver  myserver.mydomain.local  localhost.localdomain  localhost

Hope it helps!

While true and thorough, this looks up the local systems's hostname/fqdn. The OP asked how to lookup external dns names for site scraping. Still +1 for the info. – VooDooNOFX Jun 3, 2014 at 21:34 When I get FQDN's I get maybe half the servers with full FQDNs and the rest are just hostname. nothing. Why? Nslookup shows the FQDN on those nothing servers. – Nathan McKaskle May 8, 2019 at 19:41 @LaurentLAPORTE we have multiple dns domains and thousands of servers in different domains, I'm trying to get the fqdn from a hostname, it's all coming back with just "hostname." My hosts looks like 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 I dunno what's going on. – Nathan McKaskle May 10, 2019 at 19:58

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

We can also call socket.gethostbyname('google.com'). docs.python.org/2/library/socket.html#socket.gethostbyname – HarlemSquirrel Aug 20, 2018 at 15:17 gethostbyname() does not support IPv6 name resolution, and getaddrinfo() should be used instead for IPv4/v6 dual stack support. – Christoph Lösch Jul 21, 2019 at 1:22

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)
import socket
addr1 = socket.gethostbyname('google.com')
addr2 = socket.gethostbyname('yahoo.com')
print(addr1, addr2)
                gethostbyname() does not support IPv6 name resolution, and getaddrinfo() should be used instead for IPv4/v6 dual stack support.
– Christoph Lösch
                Jul 21, 2019 at 1:22
>>> 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.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.