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)})
–
–
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!
–
–
–
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
–
–
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)
–
>>> 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.