様々なコマンドを使って確認するのは面倒臭いので python スクリプトを作りました。
check_time_server.py として保存してください。
import argparse
import socket
import sys
CHECK = {None: '--', False: 'NG', True: 'OK'}
PORT = 123
flag_dryrun = False
flag_verbose = False
timeout_second = 1
message = print
def verbose(msg):
return flag_verbose and message(msg)
def try_sntp(addr):
pkt = bytearray(4*12)
pkt[0] = (3 << 3) | 3
ipaddr = addr[4][0]
sock = socket.socket(family=addr[0], type=addr[1], proto=socket.IPPROTO_UDP)
if timeout_second > 0:
sock.settimeout(timeout_second)
try:
sres = sock.sendto(pkt, (ipaddr, PORT))
rres = sock.recvfrom(64)
except OSError as e:
message(f'Error: {e} ({ipaddr})')
sock.close()
return False
sock.close()
return True
def check_server(hostname):
message(f'Server: {hostname}')
addrs = socket.getaddrinfo(hostname, PORT, proto=socket.IPPROTO_UDP)
verbose('Address:')
hostmap = [{}, {}]
for addr in addrs:
ipaddr = addr[4][0]
try:
sname = socket.gethostbyaddr(ipaddr)[0]
except OSError as e:
verbose(f'Error: gethostbyaddr("{ipaddr}"): {e}')
sname = hostname
verbose(f' {ipaddr:39} : {sname}')
success = None
if not flag_dryrun:
success = try_sntp(addr)
hmap = hostmap[int(addr[0] == socket.AF_INET6)]
if sname not in hmap:
hmap[sname] = []
hmap[sname].append([ipaddr, success])
for host in sorted(set(sum((list(h.keys()) for h in hostmap), []))):
message('\n'.join([
f'{host}:',
*[f' {CHECK[check]} - {addr}' for hmap in hostmap
for addr, check in sorted(hmap.get(host, []))]]))
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-v', '--verbose', action='store_true')
parser.add_argument('-d', '--dryrun', action='store_true')
parser.add_argument('-t', '--timeout', type=int, default=1)
parser.add_argument('server', metavar='SERVER')
args = parser.parse_args()
flag_dryrun = args.dryrun
flag_verbose = args.verbose
timeout_second = args.timeout
check_server(args.server)
次のような結果が得られるでしょう。
$ python3 check_time_server.py time.apple.com
Server: time.apple.com
Error: timed out (2403:300:a0c:4000::1f2)
Error: timed out (2403:300:a0c:3000::1e2)
Error: timed out (2403:300:a16:4000::1f2)
jptyo5-ntp-001.aaplimg.com:
OK - 17.253.68.125
jptyo5-ntp-002.aaplimg.com:
OK - 17.253.68.253
NG - 2403:300:a0c:4000::1f2
jptyo5-ntp-003.aaplimg.com:
NG - 2403:300:a0c:3000::1e2
jptyo5-ntp-004.aaplimg.com:
OK - 17.253.68.251
krsel6-ntp-002.aaplimg.com:
NG - 2403:300:a16:4000::1f2
$ python3 check_time_server.py time.apple.com
Server: time.apple.com
Error: timed out (2403:300:a0c:4000::1f2)
Error: timed out (2403:300:a0c:3000::1e2)
jptyo5-ntp-001.aaplimg.com:
OK - 17.253.68.125
jptyo5-ntp-002.aaplimg.com:
OK - 17.253.68.253
NG - 2403:300:a0c:4000::1f2
jptyo5-ntp-003.aaplimg.com:
NG - 2403:300:a0c:3000::1e2
jptyo5-ntp-004.aaplimg.com:
OK - 17.253.68.251
krsel6-ntp-002.aaplimg.com:
OK - 2403:300:a16:4000::1f2
ちなみに、 ntp.nict.jp, time.google.com , time.aws.com など公開 NTP で NG が出るのは見た事がないです。