nslookup not properly picking up domains in list in Jupyter?

224 Views Asked by At

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.

2

There are 2 best solutions below

0
On BEST ANSWER

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 ) )
1
On
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.