NAME

winsntp 0.36 - Simple Network Time Protocol client for Windows


DESCRIPTION

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.

Why yet another SNTP client?

Because I wanted something smaller and simpler than what I had found, and wanted output to a log file when running from a scheduler.

Why in Perl, which is not as fast as a compiled program?

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.

Why for Windows only?

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.


PREREQUISITES

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


BUGS

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...


CONFIGURATION

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

  @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.

$verbosity

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.

$interactive

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.

$debug

0 or 1. Set it to 1 if you have problems, and send me the results.

$use_NT_Eventlog

0 or 1. If on NT, also write results to Eventlog (even when run interactively). Ignored on Win9x.

$force_log

0 or 1. Force writing also to logfile when $interactive is 1.

$logfile

Name of logfile. Default is $0 . ``.log'' (the script name with ``.log'' appended to it).

$max_net_lag

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.

$max_diff

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 :-).

$num_samples

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

Timeout waiting for answer, in seconds. Default is 2.

$set_time

0 or 1. If 0, only displays the difference, but doesn't correct the clock.


NOTES

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.


SCRIPT CATEGORIES

Win32


OSNAMES

MSWin32


AUTHOR, COPYRIGHT, LICENSE

Copyright M. Ivkovic, 1999-2004.

perl -e "print qq(mi.perl\x40alma.ch\n)"

Same license as Perl itself.


README

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).