Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

How to check system's current NTP configuration on all OS?

by thanos1983 (Parson)
on Jul 26, 2015 at 21:45 UTC ( [id://1136368]=perlquestion: print w/replies, xml ) Need Help??

thanos1983 has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

I have been implementing a script to retrieve the current NTP configuration, based on all OS. I am using LinuxOS so I am not able to create/verify how to retrieve information in WindowsOS.

I have done my research and I think I know how to do it. Is it possible someone with WindowsOS to assist me on that?

On Linux I am applying the following command: ntpdc -c sysinfo and the output is:

system peer: 94-246-113-188.isp.djdata.se system peer mode: client leap indicator: 00 stratum: 3 precision: -23 root distance: 0.04117 s root dispersion: 0.05130 s reference ID: [94.246.113.188] reference time: d95fccb6.9c21530b Sun, Jul 26 2015 23:16:38.609 system flags: auth monitor ntp kernel stats jitter: 0.001999 s stability: 0.000 ppm broadcastdelay: 0.000000 s authdelay: 0.000000 s

In theory just by applying the following command w32tm /query /status will give you the following information such as:

stratum leap indicator precision last sync NTP server poll interval

In case of trouble shouting of the following command on windows you can find more information and the solution from here (How can I check a system's current NTP configuration?)

My script is working on LinuxOS and I assume it should work also on MacOS (since they are based on Linux), but it would be nice if someone could verify that for me.

Update: Modifying split function from push @ntpdc, split (/:/, $element); to push @ntpdc, split (/:/, $element,2);. The reason is that we want to capture only the first delimiter : so I set the split limit. I will also update the output of the script.

Update 2: Modifying regex for removing white space regex $element =~ s/\s//g; to $element =~ s/\s\s+/ /g; and also modifying the split function from push @ntpdc, split (/:/, $element,2); to push @ntpdc, split (/: /, $element,2);. I am also decoding the message to human readable format with each key and value in place and adding the new output.

Script and output:

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; if ( $^O eq 'MSWin32' ) { } elsif ( $^O eq 'WinXP' ) { } else { my @ntpdate_tmp = `ntpdc -c sysinfo`; my %ntpdc = setKeyAndValue(@ntpdate_tmp); ($ntpdc{"reference time"}, $ntpdc{"tmp"}) = split(/ /, $ntpdc{"ref +erence time"}, 2); delete $ntpdc{"tmp"}; ( $ntpdc{"Reference Timestamp Sec"}, $ntpdc{"Reference Timestamp Micro Sec"} ) = split(/\./,$ntpdc{"r +eference time"}); delete $ntpdc{"reference time"}; $ntpdc{"Reference Timestamp Sec"} = hex($ntpdc{"Reference Timestamp Sec"}) - 2208988800; $ntpdc{"Reference Timestamp Micro Sec"} = hex($ntpdc{"Reference Ti +mestamp Micro Sec"}); chop($ntpdc{"root distance"}); chop($ntpdc{"root dispersion"}); ($ntpdc{"root dispersion sec"}, $ntpdc{"root dispersion fraction"} +) = split(/\./,$ntpdc{"root dispersion"}); ($ntpdc{"root distance sec"}, $ntpdc{"root distance fraction"}) = +split(/\./,$ntpdc{"root distance"}); print Dumper \%ntpdc; } sub setKeyAndValue { my @KeyAndValue = @_; @KeyAndValue = map { s/^\s+|\s+$//g; $_; } @KeyAndValue; my @ntpdc = (); foreach my $element (@KeyAndValue) { $element =~ s/\s\s+/ /g; push @ntpdc, split(/: /, $element, 2); } my %ntpdcTmp = @ntpdc; return %ntpdcTmp; } __END__ $VAR1 = { 'Reference Timestamp Micro Sec' => 276843474, 'stability' => '0.000 ppm', 'system peer mode' => 'client', 'root distance' => '0.03453 ', 'root distance sec' => '0', 'Reference Timestamp Sec' => 1438043541, 'reference ID' => '[94.246.113.188]', 'authdelay' => '0.000000 s', 'root distance fraction' => '03453 ', 'precision' => '-23', 'root dispersion fraction' => '04585 ', 'broadcastdelay' => '0.000000 s', 'stratum' => '3', 'system peer' => '94-246-113-188.isp.djdata.se', 'system flags' => 'auth monitor ntp kernel stats', 'root dispersion' => '0.04585 ', 'root dispersion sec' => '0', 'leap indicator' => '00', 'jitter' => '0.000504 s' };

Previous output before second Update:

$VAR1 = { 'leapindicator' => '00', 'rootdispersion' => '0.05389s', 'rootdistance' => '0.03000s', 'systempeer' => '94-246-113-188.isp.djdata.se', 'systemflags' => 'authmonitorntpkernelstats', 'authdelay' => '0.000000s', 'stability' => '0.000ppm', 'jitter' => '0.002380s', 'systempeermode' => 'client', 'precision' => '-23', 'stratum' => '3', 'referenceID' => '[94.246.113.188]', 'referencetime' => 'd95fd604.9bccbe66Sun,Jul26201523:56:20.6 +08', 'broadcastdelay' => '0.000000s' };

Faulty output before updating the split function:

$VAR1 = { 'systemflags' => 'authmonitorntpkernelstats', 'rootdistance' => '0.02570s', 'referenceID' => '[83.168.200.199]', 'broadcastdelay' => '0.000000s', 'precision' => '-23', 'jitter' => '0.001984s', 'systempeermode' => 'client', 'stability' => '0.000ppm', 'systempeer' => 'ntp3.flashdance.cx', 'leapindicator' => '00', 'authdelay' => '0.000000s', 'stratum' => '3', 'rootdispersion' => '0.04956s', 'referencetime' => 'd95fc917.9be1e269Sun,Jul26201523', '01' => '11.608' };

So the question is, can someone actually test the command works with WindowsOS and provide sample of the output?

Thank you in advance for your time and effort, reading and replying to my question.

Seeking for Perl wisdom...on the process of learning...not there...yet!

Replies are listed 'Best First'.
Re: How to check system's current NTP configuration on all OS?
by 1nickt (Canon) on Jul 26, 2015 at 23:43 UTC

    Here's what I got on Mountain Lion:

    [16:40][nick:~/monks]$ perl -v | head -2; perl -M5.010 -e "say '$^O: ' +, $^O;say;" This is perl 5, version 16, subversion 0 (v5.16.0) built for darwin-2l +evel $^O: darwin [16:40][nick:~/monks]$ perl 1136368.pl $VAR1 = { 'authdelay' => '0.000000s', 'broadcastdelay' => '0.000000s', 'jitter' => '0.000000s', 'leapindicator' => '11', 'precision' => '-20', 'referenceID' => '[73.78.73.84]', 'referencetime' => '00000000.00000000Sun,Dec31189916:00:00.0 +00', 'rootdispersion' => '1.54663s', 'rootdistance' => '0.00000s', 'stability' => '0.000ppm', 'stratum' => '16', 'systemflags' => 'authmonitorntpkernelstats', 'systempeer' => 'localhost', 'systempeermode' => 'unspec' };
    The way forward always starts with a minimal test.

      Hello 1nickt,

      That is perfect. Based on your output I can understand that you are using a MacOS based on from perlport darwin is (Mac OS X).

      Thank you for your time and effort, it is very important for me the compatibility with all OS.

      Seeking for Perl wisdom...on the process of learning...not there...yet!
Re: How to check system's current NTP configuration on all OS?
by Discipulus (Canon) on Jul 27, 2015 at 07:52 UTC
    dunno if the output of such commands can be still useful to you..

    Notice that on win you must be Administrator to run the above commands.

    L*
    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

      Hello Discipulus,

      Thank you for your time and effort, reading, testing and replying to my question.

      The part that I am interested is:

      C:\Windows\system32>w32tm.exe /query /status Indicatore di secondo intercalare: 0(nessun avviso) Strato: 4 (riferimento secondario - sincronizza tramite (S)NTP) Precisione: -6 (15.625ms per tick) Ritardo radice: 0.0937500s Dispersione radice: 1.4511151s ID riferimento: 0xAC180106 (IP origine: 172.24.1.6) Data e ora dell'ultima sincronizzazione riuscita: 27/07/2015 07:48:49 Origine: DC04-RM.MC-link.HQ Intervallo di polling: 15 (32768s)

      Damn I was afraid that it might require to be Administrator to get these values. :( Well I have to find an alternative solution I assume.

      Thank you again for your time and effort.

      Seeking for Perl wisdom...on the process of learning...not there...yet!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1136368]
Approved by kcott
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2024-04-18 20:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found