Skip to content

Instantly share code, notes, and snippets.

@aewallin
Created November 29, 2024 15:02
Show Gist options
  • Save aewallin/79f5d9cc2b22585adea1e81b03f26899 to your computer and use it in GitHub Desktop.
Save aewallin/79f5d9cc2b22585adea1e81b03f26899 to your computer and use it in GitHub Desktop.
NTP query
"""
NTPLib query NTP-servers
"""
import ntplib
import datetime
def test_ntp(ip):
c = ntplib.NTPClient()
r=c.request(ip)
d = datetime.datetime.utcfromtimestamp(r.recv_time)
print('%s ofs(ms) %.3f dly(ms) %.3f stratum %d recv_time %s'%(ip, 1e3*r.offset, 1e3*r.root_delay, r.stratum, d))
test_ntp('time.mikes.fi')
test_ntp('time1.mikes.fi')
test_ntp('time2.mikes.fi')#test_ntp('time3.mikes.fi')
test_ntp('194.100.49.147')
test_ntp('ntp1.mikes.fi')
test_ntp('ntp2.mikes.fi')
test_ntp('ntp3.mikes.fi')
test_ntp('ntp4.mikes.fi')
test_ntp('ntp1.mikes.funet.fi')
test_ntp('ntp2.mikes.funet.fi')
test_ntp('ntp3.mikes.funet.fi')
test_ntp('ntp4.mikes.funet.fi')
"""
Output:
time.mikes.fi ofs(ms) 8.877 dly(ms) 0.214 stratum 2 recv_time 2024-11-29 14:59:18.945403
time1.mikes.fi ofs(ms) 8.828 dly(ms) 0.229 stratum 2 recv_time 2024-11-29 14:59:18.949283
time2.mikes.fi ofs(ms) 8.933 dly(ms) 0.214 stratum 2 recv_time 2024-11-29 14:59:18.953479
194.100.49.147 ofs(ms) 9.325 dly(ms) 0.244 stratum 2 recv_time 2024-11-29 14:59:18.956861
ntp1.mikes.fi ofs(ms) 9.028 dly(ms) 0.000 stratum 1 recv_time 2024-11-29 14:59:18.960679
ntp2.mikes.fi ofs(ms) 8.881 dly(ms) 0.000 stratum 1 recv_time 2024-11-29 14:59:18.965340
ntp3.mikes.fi ofs(ms) 9.375 dly(ms) 0.000 stratum 1 recv_time 2024-11-29 14:59:18.969815
ntp4.mikes.fi ofs(ms) 9.288 dly(ms) 0.000 stratum 1 recv_time 2024-11-29 14:59:18.973725
ntp1.mikes.funet.fi ofs(ms) 9.533 dly(ms) 0.000 stratum 1 recv_time 2024-11-29 14:59:18.977633
ntp2.mikes.funet.fi ofs(ms) 9.089 dly(ms) 0.000 stratum 1 recv_time 2024-11-29 14:59:18.980698
ntp3.mikes.funet.fi ofs(ms) 9.662 dly(ms) 0.000 stratum 1 recv_time 2024-11-29 14:59:18.984259
ntp4.mikes.funet.fi ofs(ms) 9.152 dly(ms) 0.000 stratum 1 recv_time 2024-11-29 14:59:18.987779
"""
"""
ref_timestamp Reference Timestamp Time when the system clock was last set or corrected, in NTP timestamp format
orig_timestamp originate timestamp (T1) Time at the client when the request departed for the server, in NTP timestamp format.
recv_timestamp receive timestamp (T2) Time at the server when the request arrived from the client, in NTP timestamp format.
tx_timestamp transmit timestamp (T3) Time at the server when the response left for the client, in NTP timestamp format.
dest_timestamp Destination timestamp (T4) Time at the client when the reply arrived from the server, in NTP timestamp format.
"""
"""
['leap',
'version',
'mode',
'stratum',
'poll',
'precision',
'root_delay',
'root_dispersion',
'ref_id',
'ref_timestamp',
'orig_timestamp',
'recv_timestamp',
'tx_timestamp',
'dest_timestamp',
'__module__',
'__doc__',
'__init__',
'offset',
'delay',
'tx_time',
'recv_time',
'orig_time',
'ref_time',
'dest_time',]
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment