Skip to content

Instantly share code, notes, and snippets.

@crashdump
Last active December 29, 2022 16:17
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save crashdump/5704286 to your computer and use it in GitHub Desktop.
Save crashdump/5704286 to your computer and use it in GitHub Desktop.
Compare the time of two ntp servers.
#!/usr/bin/python
import struct, sys, time, argparse
from socket import socket, AF_INET, SOCK_DGRAM
error_margin = 3
def get_time(host):
sock = socket(AF_INET, SOCK_DGRAM)
sock.settimeout(2)
sock.sendto('\x1b' + 47 * '\0', (host, 123))
data, address = sock.recvfrom( 1024 )
sock.close()
if data:
t = struct.unpack( '!12I', data )[10]
t -= 2208988800L # Time 1970
return t
else:
return False
def main():
parser = argparse.ArgumentParser()
parser.add_argument('time_server', help='specify the ntp server you want to check')
parser.add_argument('time_reference', help='specify the time reference (another ntp server)')
parser.add_argument('-v', '--verbose', help='talk a lot more', action='store_true')
args = parser.parse_args()
time_server = get_time(args.time_server)
time_reference = get_time(args.time_reference)
if time_server and time_reference:
if args.verbose:
print 'Time received from %s = %s (%s)' % (args.time_server, time_server, time.ctime(time_server))
print 'Time received from %s = %s (%s)' % (args.time_reference, time_reference, time.ctime(time_reference))
# if s2 <= s1 + err and s2 >= s1 - err
if time_server <= time_reference + error_margin and time_server >= time_reference - error_margin:
print 'Times match.'
else:
print 'Times differ.'
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment