#!/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{"reference time"}, 2); delete $ntpdc{"tmp"}; ( $ntpdc{"Reference Timestamp Sec"}, $ntpdc{"Reference Timestamp Micro Sec"} ) = split(/\./,$ntpdc{"reference time"}); delete $ntpdc{"reference time"}; $ntpdc{"Reference Timestamp Sec"} = hex($ntpdc{"Reference Timestamp Sec"}) - 2208988800; $ntpdc{"Reference Timestamp Micro Sec"} = hex($ntpdc{"Reference Timestamp 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' };