winsntp 0.36 - Simple Network Time Protocol client for Windows
This Perl script uses SNTP (Simple Network Time Protocol, RFC 2030) to get the time from an NTP server and set the Windows clock.
It can be used interactively or from a scheduler, and can produce output to a log file and/or the Windows NT Event log.
Now that Windows 2000 and XP's can use SNTP servers (see NET HELP TIME
),
it's probably only useful on old NT and 9x systems.
Because I wanted something smaller and simpler than what I had found, and wanted output to a log file when running from a scheduler.
Because I like Perl, it was well suited to the task, and it turns out to be fast enough: I seem to get a precision in the range of 50 ms. on a Pentium 150 with Windows 95.
Because I only need it on Win32 (I use ntpdate on Linux), and because I don't know how to get/set the time with sub-second precision on other systems. If you need it on another system, and you know how to do it, let me know.
This script needs the <Win32::API> module, available through PPM:
ppm install Win32-API
or at Aldo Calpini's site:
http://www.divinf.it/dada/perl/Win32API-0.011.zip
The Leap Indicator for a leap second in the last minute of the day is ignored. Which means that you'll be off by 1 second on January the 1st of the years following the occasional years ending with a 59 or 61 seconds minute.
The code is not Y2.036K compliant :-). If you are still using this script in 2036, read RFC 2030 and fix the code yourself, since I may not be available any more to do it (unless I stop smoking, maybe?). (And send me an e-mail with your fix, just in case I'm still alive after all).
Let me know about others...
These configuration options can be placed in a configuration file. The file name can be given as argument to the script. If there is no argument, a file named script_file_name.cfg (like in ``winsntp.pl.cfg'') is tried. If the file is not found, default values are supplied in the script.
The file is require'd in the script, so it's format is Perl, and it should end with a true value (just put 1; at the end).
Here is a sample configuration file content:
@timehosts = qw( bernina.ethz.ch swisstime.ethz.ch); $verbosity = 1; $interactive = 1; $debug = 0; $use_NT_Eventlog = 1; $force_log = 1; $logfile = $0 . ".log"; $max_net_lag = 1000; $max_diff = 86400; $num_samples = 5; $timeout = 2; $set_time = 1; 1;
@timehosts = qw( swisstime.ethz.ch bernina.ethz.ch );
List of time hosts to use. The full lists are available at http://www.eecis.udel.edu/~mills/ntp/servers.html. If a host cannot be reached, the next one is used etc...
Use stratum 2 hosts. Don't bother stratum 1 hosts for a precision you don't need and won't get anyway.
How much is printed to the screen or log. 0: nothing. Normally set to 1 or 2. Up to 7 for lots of progress messages.
0 or 1. If not interactive, writes results to log file. If interactive, writes to screen. Also writes a bit more stuff in interactive mode ($verbosity = $verbosity + $interactive). Set it to 0 when running from a scheduler.
0 or 1. Set it to 1 if you have problems, and send me the results.
0 or 1. If on NT, also write results to Eventlog (even when run interactively). Ignored on Win9x.
0 or 1. Force writing also to logfile when $interactive is 1.
Name of logfile. Default is $0 . ``.log'' (the script name with ``.log'' appended to it).
Maximum milliseconds to get an answer from the time host. If it takes longer, ignores the answer and tries again. Set it to 1000 for 1 second.
Maximum time difference in seconds. If the difference is greater, we don't set the time, fearing there may have been some weird error. 86400 = 1 day. Normal errors are caught by looking at the NTP message, as described in RFC: LI is 3 or time is 0. But this will prevent setting the clock to something stupid in February 2036 :-).
How many samples to get from host, so we can choose the best one (the one with the shortest round trip delay). Probably unnecessary.
Timeout waiting for answer, in seconds. Default is 2.
0 or 1. If 0, only displays the difference, but doesn't correct the clock.
You can use this freely.
I would appreciate a short (or long) e-mail note if you do. And of course, bug-reports and/or improvements are welcome.
Last revision: 07.03.2004. Latest version should be available at http://alma.ch/perl/scripts/ and/or on CPAN under scripts.
Win32
MSWin32
Copyright M. Ivkovic, 1999-2004.
perl -e "print qq(mi.perl\x40alma.ch\n)"
Same license as Perl itself.
This Perl script uses SNTP (Simple Network Time Protocol, RFC 2030) to get the time from an NTP server and set the Windows clock.
It can be used interactively or from a scheduler, and can produce output to a log file and/or the Windows NT Event log.
It needs Aldo Calpini's Win32::API module (see PREREQUISITES).