| 1 |
#!/usr/bin/env python2.7 |
| 2 |
# Copyright 2015 gRPC authors. |
| 3 |
# |
| 4 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
# you may not use this file except in compliance with the License. |
| 6 |
# You may obtain a copy of the License at |
| 7 |
# |
| 8 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
# |
| 10 |
# Unless required by applicable law or agreed to in writing, software |
| 11 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
# See the License for the specific language governing permissions and |
| 14 |
# limitations under the License. |
| 15 |
"""Makes DNS queries for A records to specified servers""" |
| 16 |
|
| 17 |
import argparse |
| 18 |
import threading |
| 19 |
import time |
| 20 |
|
| 21 |
import twisted.internet.reactor as reactor |
| 22 |
import twisted.internet.task as task |
| 23 |
import twisted.names.client as client |
| 24 |
|
| 25 |
|
| 26 |
def main(): |
| 27 |
argp = argparse.ArgumentParser(description='Make DNS queries for A records') |
| 28 |
argp.add_argument('-s', |
| 29 |
'--server_host', |
| 30 |
default='127.0.0.1', |
| 31 |
type=str, |
| 32 |
help='Host for DNS server to listen on for TCP and UDP.') |
| 33 |
argp.add_argument('-p', |
| 34 |
'--server_port', |
| 35 |
default=53, |
| 36 |
type=int, |
| 37 |
help='Port that the DNS server is listening on.') |
| 38 |
argp.add_argument('-n', |
| 39 |
'--qname', |
| 40 |
default=None, |
| 41 |
type=str, |
| 42 |
help=('Name of the record to query for. ')) |
| 43 |
argp.add_argument('-t', |
| 44 |
'--timeout', |
| 45 |
default=1, |
| 46 |
type=int, |
| 47 |
help=('Force process exit after this number of seconds.')) |
| 48 |
args = argp.parse_args() |
| 49 |
|
| 50 |
def OnResolverResultAvailable(result): |
| 51 |
answers, authority, additional = result |
| 52 |
for a in answers: |
| 53 |
print(a.payload) |
| 54 |
|
| 55 |
def BeginQuery(reactor, qname): |
| 56 |
servers = [(args.server_host, args.server_port)] |
| 57 |
resolver = client.Resolver(servers=servers) |
| 58 |
deferred_result = resolver.lookupAddress(args.qname) |
| 59 |
deferred_result.addCallback(OnResolverResultAvailable) |
| 60 |
return deferred_result |
| 61 |
|
| 62 |
task.react(BeginQuery, [args.qname]) |
| 63 |
|
| 64 |
|
| 65 |
if __name__ == '__main__': |
| 66 |
main() |