Skip to content

Instantly share code, notes, and snippets.

@Antti
Created May 22, 2012 10:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Antti/2768098 to your computer and use it in GitHub Desktop.
Save Antti/2768098 to your computer and use it in GitHub Desktop.
Simple NTP query
require 'socket'
s = UDPSocket.new
NTP_PACKET_SIZE= 48
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, "pool.ntp.org", 123
response = s.recv(NTP_PACKET_SIZE)
z, time = response.unpack("A40N")
puts "Time received: #{time} seconds since 1900"
epoch = time - EPOCH_SECONDS
p Time.at epoch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment