-
Notifications
You must be signed in to change notification settings - Fork 0
/
ns_lookup.py
executable file
·72 lines (62 loc) · 1.7 KB
/
ns_lookup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/python3
#
# Simple python script to check if a domain is pointed to certain
# DNS nameservers, e.g. ns1.example.com and ns2.example.com
#
# The script takes one positional argument, which is a file.
# The file must contain the domains, which you are querying for.
# They are separated by newline.
#
# Example: ./ns_lookup.py domains.txt
#
# You will also need to fill in the list "name_servers" with the nameservers,
# which you are mathing each domain against.
#
# Author: Valentin Dzhorov
#
import dns.resolver
from sys import argv, exit
script, inputFile = argv
class Lookup(object):
def __init__(self, inputFile, nameServers=[]):
self.inputFile = inputFile
self.nameServers = nameServers
def strip_newline(self, line):
return(line.strip())
def read_file(self):
fileHandle = None
try:
fileHandle = open(self.inputFile, mode='r')
except FileNotFoundError:
print("File does not exist")
exit(1)
except PermissionError:
print("Cannot open file, permission denied.")
exit(1)
domainList = fileHandle.readlines()
fileHandle.close()
return(domainList)
def perform_query(self):
domainList = self.read_file()
for name in domainList:
name = self.strip_newline(name)
try:
answer = dns.resolver.query(name, 'NS', raise_on_no_answer=False)
except dns.resolver.NXDOMAIN:
print(f"{name}")
except dns.resolver.NoNameservers:
print(f"{name} servfail")
continue
if answer.rrset is not None:
try:
for ns in self.nameServers:
str(answer.rrset).index(ns)
except ValueError:
print(f"{name}")
else:
pass
else:
pass
name_servers = ['ns1.delta.bg', 'ns2.delta.bg']
query = Lookup(inputFile, name_servers)
query.perform_query()