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!