forked from knightswd/NoPacScan
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdnsresolve.py
More file actions
56 lines (49 loc) · 1.49 KB
/
dnsresolve.py
File metadata and controls
56 lines (49 loc) · 1.49 KB
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
#!/usr/bin/env python
"""
Python DNS Client
(C) 2014 David Lettier
lettier.com
A simple DNS client similar to `nslookup` or `host`.
Does not use any DNS libraries.
Handles only A type records.
"""
import sys
import socket
import aiodns
import asyncio
import pycares
class DNSResolve:
def __init__(self, dnsIp, dnsPort):
self._dnsIp = dnsIp
self._dnsPort = dnsPort
def DNSResolve(self, host_name_to, qtype):
ip = []
ip.append(self._dnsIp)
try:
loop = asyncio.new_event_loop()
resolver = aiodns.DNSResolver(loop=loop)
resolver.nameservers = ip
f = resolver.query(host_name_to, qtype)
result = loop.run_until_complete(f)
except Exception as e:
return None
return result
def getDCIp(self, domain, type="ANY"):
domain = '_msdcs.' + domain
# Convert data to bit string.
dnsAnswer = self.DNSResolve(domain, type)
items = []
for data in dnsAnswer:
if isinstance(data, pycares.ares_query_ns_result):
items.append(str(data.host))
elif isinstance(data, pycares.ares_query_soa_result):
items.append(str(data.nsname))
else:
print("there have other type")
items = list(set(items))
ips = []
for dnsDomain in items:
ip = self.DNSResolve(dnsDomain, "A")
if ip:
ips.append(str(ip[0].host))
return ips