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 17import argparse 18import threading 19import time 20import twisted.internet.task as task 21import twisted.names.client as client 22import twisted.internet.reactor as reactor 23 24 25def main(): 26 argp = argparse.ArgumentParser(description='Make DNS queries for A records') 27 argp.add_argument('-s', 28 '--server_host', 29 default='127.0.0.1', 30 type=str, 31 help='Host for DNS server to listen on for TCP and UDP.') 32 argp.add_argument('-p', 33 '--server_port', 34 default=53, 35 type=int, 36 help='Port that the DNS server is listening on.') 37 argp.add_argument('-n', 38 '--qname', 39 default=None, 40 type=str, 41 help=('Name of the record to query for. ')) 42 argp.add_argument('-t', 43 '--timeout', 44 default=1, 45 type=int, 46 help=('Force process exit after this number of seconds.')) 47 args = argp.parse_args() 48 49 def OnResolverResultAvailable(result): 50 answers, authority, additional = result 51 for a in answers: 52 print(a.payload) 53 54 def BeginQuery(reactor, qname): 55 servers = [(args.server_host, args.server_port)] 56 resolver = client.Resolver(servers=servers) 57 deferred_result = resolver.lookupAddress(args.qname) 58 deferred_result.addCallback(OnResolverResultAvailable) 59 return deferred_result 60 61 task.react(BeginQuery, [args.qname]) 62 63 64if __name__ == '__main__': 65 main() 66