/usr/bin/ntptrace is in ntpsec 1.1.0+dfsg1-1.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | #!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
ntptrace - trace peers of an NTP server
Usage: ntptrace [-n | --numeric] [-m number | --max-hosts=number]
[-r hostname | --host=hostname] [--help | --more-help]
hostname
See the manual page for details.
"""
# SPDX-License-Identifier: BSD-2-Clause
from __future__ import print_function
import getopt
import re
import subprocess
import sys
try:
import ntp.util
except ImportError as e:
sys.stderr.write(
"ntptrace: can't find Python NTP library.\n")
sys.stderr.write("%s\n" % e)
sys.exit(1)
def get_info(host):
info = ntp_read_vars(0, [], host)
if info is None or 'stratum' not in info:
return
info['offset'] = round(float(info['offset']) / 1000, 6)
info['syncdistance'] = \
(float(info['rootdisp']) + (float(info['rootdelay']) / 2)) / 1000
return info
def get_next_host(peer, host):
info = ntp_read_vars(peer, ["srcadr"], host)
if info is None:
return
return info['srcadr']
def ntp_read_vars(peer, vars, host):
obsolete = {'phase': 'offset',
'rootdispersion': 'rootdisp'}
if not len(vars):
do_all = True
else:
do_all = False
outvars = {}.fromkeys(vars)
if do_all:
outvars['status_line'] = {}
cmd = ["ntpq", "-n", "-c", "rv %s %s" % (peer, ",".join(vars))]
if host is not None:
cmd.append(host)
try:
# sadly subprocess.check_output() is not in Python 2.6
proc = subprocess.Popen(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
out = proc.communicate()[0]
output = out.decode('utf-8').splitlines()
except subprocess.CalledProcessError as e:
print("Could not start ntpq: %s" % e.output, file=sys.stderr)
raise SystemExit(1)
except OSError as e:
print("Could not start ntpq: %s" % e.strerror, file=sys.stderr)
raise SystemExit(1)
for line in output:
if re.search(r'Connection refused', line):
return
match = re.search(r'^asso?c?id=0 status=(\S{4}) (\S+), (\S+),', line,
flags=re.IGNORECASE)
if match:
outvars['status_line']['status'] = match.group(1)
outvars['status_line']['leap'] = match.group(2)
outvars['status_line']['sync'] = match.group(3)
iterator = re.finditer(r'(\w+)=([^,]+),?\s?', line)
for match in iterator:
key = match.group(1)
val = match.group(2)
val = re.sub(r'^"([^"]+)"$', r'\1', val)
if key in obsolete:
key = obsolete[key]
if do_all or key in outvars:
outvars[key] = val
return outvars
usage = r"""ntptrace - trace peers of an NTP server
USAGE: ntptrace [-<flag> [<val>] | --<name>[{=| }<val>]]... [host]
-n, --numeric Print IP addresses instead of hostnames
-m, --max-hosts=num Maximum number of peers to trace
-r, --host=str Single remote host
-?, --help Display usage information and exit
--more-help Pass the extended usage text through a pager
Options are specified by doubled hyphens and their name or by a single
hyphen and the flag character.""" + "\n"
try:
(options, arguments) = getopt.getopt(
sys.argv[1:], "m:nr:?",
["help", "host=", "max-hosts=", "more-help", "numeric"])
except getopt.GetoptError as err:
sys.stderr.write(str(err) + "\n")
raise SystemExit(1)
numeric = False
maxhosts = 99
host = '127.0.0.1'
for (switch, val) in options:
if switch == "-m" or switch == "--max-hosts":
errmsg = "Error: -m parameter '%s' not a number\n"
maxhosts = ntp.util.safeargcast(val, int, errmsg, usage)
elif switch == "-n" or switch == "--numeric":
numeric = True
elif switch == "-r" or switch == "--host":
host = val
elif switch == "-?" or switch == "--help" or switch == "--more-help":
print(usage, file=sys.stderr)
raise SystemExit(0)
if len(arguments):
host = arguments[0]
hostcount = 0
while True:
hostcount += 1
info = get_info(host)
if info is None:
break
if not numeric:
host = ntp.util.canonicalize_dns(host)
print("%s: stratum %d, offset %f, synch distance %f" %
(host, int(info['stratum']), info['offset'], info['syncdistance']),
end='')
if int(info['stratum']) == 1:
print(", refid '%s'" % info['refid'], end='')
print()
if (int(info['stratum']) == 0 or int(info['stratum']) == 1 or
int(info['stratum']) == 16):
break
if re.search(r'^127\.127\.\d{1,3}\.\d{1,3}$', info['refid']):
break
if hostcount == maxhosts:
break
next_host = get_next_host(info['peer'], host)
if next_host is None:
break
if re.search(r'^127\.127\.\d{1,3}\.\d{1,3}$', next_host):
break
host = next_host
|