Skip to content
Create a gist now

Instantly share code, notes, and snippets.

@Antti /ntp.rb

Simple NTP query
require 'socket'
s =
EPOCH_SECONDS = 2208988800
ntppacket = [0b11100011, 0, 6, 0xEC, 0, 49, 0x4E, 49, 52,0,0,0,0].pack("C4QC4Q4")
puts "Packet size matches: #{ntppacket.bytes.count == NTP_PACKET_SIZE}"
s.send ntppacket, 0, "", 123
response = s.recv(NTP_PACKET_SIZE)
z, time = response.unpack("A40N")
puts "Time received: #{time} seconds since 1900"
epoch = time - EPOCH_SECONDS
p epoch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.