1#! @PATH_PERL@ -w 2 3die "perl5 needed\n" unless ($] > 5); 4 5use Getopt::Std; 6 7$opt_n = 1000; # How many tries before we give up? (10 min+) 8$opt_s = 6; # Seconds to sleep between tries (6s = 10/min) 9$opt_v = 0; # Be verbose? 10 11getopts('n:s:v'); 12 13$cmd = 'ntpq -c "rv 0"'; 14 15$| = 1; # Autoflush output. 16 17print "Waiting for ntpd to synchronize... " if ($opt_v); 18for ($i = 0; $i < $opt_n; ++$i) { 19 open(Q, $cmd." 2>&1 |") || die "Can't start ntpq: $!"; 20 while(<Q>) { 21 chomp; 22 # the first line should be similar to: 23 # associd=0 status=0645 leap_none, sync_ntp, ... 24 if (/^associd=0 status=(\S{4}) (\S+), (\S+),/) { 25 my $status = $1; 26 my $leap = $2; 27 my $sync = $3; 28 # print $_; 29 # print "status <$status>, leap <$leap>, sync <$sync>\n"; 30 last if ($leap =~ /(sync|leap)_alarm/); 31 if ($leap =~ /leap_(none|((add|del)_sec))/) { 32 # We could check $sync here to make sure we like the source... 33 print "\bOK!\n" if ($opt_v); 34 exit 0; 35 } 36 print "\bUnexpected 'leap' status <$leap>\n"; 37 exit 1; 38 } 39 40 if (/Connection refused/) { 41 print "\bntpd is not running!\n" if ($opt_v); 42 exit 1; 43 } 44 45 # Otherwise, we have a bigger problem. 46 print "\bUnexpected first line <$_>\n"; 47 exit 1; 48 } 49 close(Q); 50 print "\b".substr("*+:.", $i % 4, 1) if ($opt_v); 51 sleep($opt_s); 52} 53print "\bNo!\nntpd did not synchronize.\n" if ($opt_v); 54exit 1; 55