'nslookup not properly picking up domains in list in Jupyter?
In a Jupyter Notebook, I have a domain list like the following
domain_list=['google.com','example.com','thisisnotaworkingdomain12344321.com']
I intend to pass this list through a loop to check and if each domain is live by running a quick NSLookup. However, when I enter the following code:
for i in range(len(domain_list)):
! nslookup domain_list[i]
I am returned the following:
Server: dsldevice6.attlocal.net
Address: redacted
*** dsldevice6.attlocal.net can't find domain_list[i]: Non-existent domain
Server: dsldevice6.attlocal.net
Address: redacted
*** dsldevice6.attlocal.net can't find domain_list[i]: Non-existent domain
Server: dsldevice6.attlocal.net
Address: redacted
*** dsldevice6.attlocal.net can't find domain_list[i]: Non-existent domain
So it is obviously doing an nslookup for "domain_list[i]", not for the ith item in the domain list. Is there a solution that anyone could provide? I cannot identify a quick workaround.
Solution 1:[1]
Depending on what you need to do with the output, this is the kind of thing you need:
import subprocess
for domain in domain_list:
subprocess.call( ['nslookup', domain] )
Even easier is:
import socket
for domain in domain_list:
print( socket.gethostbyname( domain ) )
Solution 2:[2]
import dns.resolver
for i in domain_list:
try:
result=dns.resolver.resolve(i,'NS')
except TypeError:
domain_list.remove(i)
This seems like a solid band-aid fix to the problem.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Tim Roberts |
| Solution 2 |
