4 ms·
A Tiny NTP client
- dfc 11y agoThis is actually a tiny `sntp` client: 14. Simple Network Time Protocol (SNTP) Primary servers and clients complying with a subset of NTP, called the Simple Network Time Protocol (SNTPv4) [RFC4330], do not need to implement the mitigation algorithms described in Section 9 and following sections. SNTP is intended for primary servers equipped with a single reference clock, as well as for clients with a single upstream server and no dependent clients. The fully developed NTPv4 implementation is intended for secondary servers with multiple upstream servers and multiple downstream servers or clients. Other than these considerations, NTP and SNTP servers and clients are completely interoperable and can be intermixed in NTP subnets. https://tools.ietf.org/html/rfc5905#section-14 https://tools.ietf.org/html/rfc5905#section-14
- ghshephard 11y agoUsually an SNTP client will add the latency/2 to get the correct time - I.E. if a packet takes 350 msec to return, then you'll want to add 175 msec from the wall-clock-time in the packet to get the "current wall clock time".
- asdfjklasdf 11y agodidn't quite work for me, but was close. This worked: date -d @$((16#`printf "\xb%-47.s"|nc -uw1 ntp.metas.ch 123|xxd -s40 -l4 -p`-2208988800))
- kcvweh0d 11y agoLess than well known: you can also use the timestamp of an ICMP packet to set your clock. The "clockdiff" command, for example, can be used to find the difference.
- 101914 11y agoHere's a portable version for those of us who do not use Bash: a=$(printf '\xb%-47.s' \ |nc -uw1 ntp.metas.ch 123 \ |exec xxd -s40 -l4 -p) b=$(printf %d\\n 0x"$a") c=$((b-2208988800)) date -r$c There is a way to do the xxd step using original netcat. Included with nc was a short program called nc-data. It converts from btoa and atob for shoveling data to nc. Slower than xxd but still useful. It also shows octal, decimal and byte number. Another exercise might be a one-liner for the TAICLOCK protocol: http://cr.yp.to/proto/taiclock.txt http://cr.yp.to/proto/taiclock.txt (Alternative to SNTP with smaller packets and support for leap seconds.)
- splitbrain 11y agoSimple alternative to quickly set the current time: use the date header of any webserver: date -s "`curl -I http://www.kernel.org |grep 'Date:'|awk -F': ' '{print $2}'`"
- aexaey 11y agoIsn't that what htpd does? http://freecode.com/projects/htpd http://freecode.com/projects/htpd http://ostatic.com/htpd http://ostatic.com/htpd