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