File: convert_server_list.py

package info (click to toggle)
namebench 1.3.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, stretch, wheezy
  • size: 1,808 kB
  • ctags: 494
  • sloc: python: 4,047; sh: 116; makefile: 17
file content (87 lines) | stat: -rwxr-xr-x 2,793 bytes parent folder | download | duplicates (2)
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env python
# Copyright 2009 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Tool for checking a lot of DNS servers from stdin for possible inclusion."""

__author__ = 'tstromberg@google.com (Thomas Stromberg)'

import re
import sys
import pygeoip
sys.path.append('..')
sys.path.append('/Users/tstromberg/namebench')
import third_party
from libnamebench import nameserver_list
from libnamebench import config
from libnamebench import addr_util

geo_city = pygeoip.GeoIP('/usr/local/share/GeoLiteCity.dat')
(options, supplied_ns, global_ns, regional_ns) = config.GetConfiguration()
cfg_nameservers = global_ns + regional_ns
#cfg_nameservers = [('205.151.67.2', '205.151.67.2')]
nameservers = nameserver_list.NameServers(
    cfg_nameservers,
    timeout=30,
    health_timeout=30,
    threads=40,
    skip_cache_collusion_checks=True,
)

nameservers.PingNameServers()

for ns in nameservers:
  if ':' in ns.ip:
    details = {}
  else:
    try:
      details = geo_city.record_by_addr(ns.ip)
    except:
      pass
      
  if not details:
    details = {}
  city = details.get('city', '')
  if city:
    city = city.decode('latin-1')
  country = details.get('country_name', '')
  if country:
    country = country.decode('latin-1')
  latitude = details.get('latitude', '')
  longitude = details.get('longitude', '')
  country_code = details.get('country_code', '')
  region = details.get('region_name', '')
  if region:
    region = region.decode('latin-1')
  matches = re.search('[- ](\d+)', ns.name)
  if matches:
    instance = matches.group(1)
    ns.name = re.sub('[- ]%s' % instance, '', ns.name)
    main = u"%s=%s (%s)" % (ns.ip, ns.name, instance)
  else:
    main = u"%s=%s" % (ns.ip, ns.name)
  if 'Responded with: REFUSED' in ns.warnings:
    note = '_REFUSED_'
  elif 'a.root-servers.net.: Timeout' in ns.warnings:
    note = '_TIMEOUT_'
  elif 'No answer (NOERROR): a.root-servers.net.' in ns.warnings:
    note = '_NOANSWER_'
  elif ns.warnings:
    note = '_WARNING/%s_' % '/'.join(list(ns.warnings))
  else:
    note = '' 

  geo = '/'.join([x for x in [city, region, country] if x and not x.isdigit()])
  entry = "%-50.50s # %s, %s, %s (%s) %s" % (main, ns.hostname, latitude, longitude, geo, note)
  print entry.encode('utf-8')