1#!/usr/bin/env python
2# Copyright 2009 Google Inc. All Rights Reserved.
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"""Tool for checking a lot of DNS servers from stdin for possible inclusion."""
17
18__author__ = 'tstromberg@google.com (Thomas Stromberg)'
19
20import re
21import sys
22import pygeoip
23sys.path.append('..')
24sys.path.append('/Users/tstromberg/namebench')
25import third_party
26from libnamebench import nameserver_list
27from libnamebench import config
28from libnamebench import addr_util
29
30geo_city = pygeoip.GeoIP('/usr/local/share/GeoLiteCity.dat')
31(options, supplied_ns, global_ns, regional_ns) = config.GetConfiguration()
32cfg_nameservers = global_ns + regional_ns
33#cfg_nameservers = [('205.151.67.2', '205.151.67.2')]
34nameservers = nameserver_list.NameServers(
35    cfg_nameservers,
36    timeout=30,
37    health_timeout=30,
38    threads=40,
39    skip_cache_collusion_checks=True,
40)
41
42nameservers.PingNameServers()
43
44for ns in nameservers:
45  if ':' in ns.ip:
46    details = {}
47  else:
48    try:
49      details = geo_city.record_by_addr(ns.ip)
50    except:
51      pass
52
53  if not details:
54    details = {}
55  city = details.get('city', '')
56  if city:
57    city = city.decode('latin-1')
58  country = details.get('country_name', '')
59  if country:
60    country = country.decode('latin-1')
61  latitude = details.get('latitude', '')
62  longitude = details.get('longitude', '')
63  country_code = details.get('country_code', '')
64  region = details.get('region_name', '')
65  if region:
66    region = region.decode('latin-1')
67  matches = re.search('[- ](\d+)', ns.name)
68  if matches:
69    instance = matches.group(1)
70    ns.name = re.sub('[- ]%s' % instance, '', ns.name)
71    main = u"%s=%s (%s)" % (ns.ip, ns.name, instance)
72  else:
73    main = u"%s=%s" % (ns.ip, ns.name)
74  if 'Responded with: REFUSED' in ns.warnings:
75    note = '_REFUSED_'
76  elif 'a.root-servers.net.: Timeout' in ns.warnings:
77    note = '_TIMEOUT_'
78  elif 'No answer (NOERROR): a.root-servers.net.' in ns.warnings:
79    note = '_NOANSWER_'
80  elif ns.warnings:
81    note = '_WARNING/%s_' % '/'.join(list(ns.warnings))
82  else:
83    note = ''
84
85  geo = '/'.join([x for x in [city, region, country] if x and not x.isdigit()])
86  entry = "%-50.50s # %s, %s, %s (%s) %s" % (main, ns.hostname, latitude, longitude, geo, note)
87  print entry.encode('utf-8')
88