NTP over HTTP
Posted by Michał ‘mina86’ Nazarewicz on 16th of January 2010
Sitting in a dark office, after swearing for hours at ATI video cards I noticed time on my computer was incorrect. ‘No problem,’ I thought as I started typing ntpdate
. That’s where it struck me that our beloved IT department had blocked most of the Internet. Checking the time on a watch or a mobile phone was not an option — I had neither — nor was looking at GKrellM on another PC — that’s lame.
‘I wish there was a NTP-over-HTTP protocol’ I sighed. And then I realised there was…
If you recall that web servers include their time in response headers solution becomes clear.
httpdate="$(wget --no-cache -S -O /dev/null google.com 2>&1 | sed -n -e 's/ *Date: *//p' -eT -eq)" [ -n "$httpdate" ] && date -s "$httpdate"
Did the trick!
It won’t guarantee sub-second accuracy, but will usually work to withing a few seconds. It also requites GNU coreutils (for date
’s -s
switch) (but implementing simple RFC1123 date parser is not that hard), wget (which can however be replaced by other tools including telnet) and sed (which is POSIX utility and also can be replaced by a 13-line C program).
Implementing a complete tool in C, C++, Rust or similar is left as a simple exercise for the reader. (Less ambitious reader may limit themselves to some scripting language with regexes).