3 die "perl5 needed\n" unless ($] > 5);
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?
13 $cmd = 'ntpq -c "rv 0"';
15 $| = 1; # Autoflush output.
17 print "Waiting for ntpd to synchronize... " if ($opt_v);
18 for ($i = 0; $i < $opt_n; ++$i) {
19 open(Q, $cmd." 2>&1 |") || die "Can't start ntpq: $!";
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+),/) {
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);
36 print "\bUnexpected 'leap' status <$leap>\n";
40 if (/Connection refused/) {
41 print "\bntpd is not running!\n" if ($opt_v);
45 # Otherwise, we have a bigger problem.
46 print "\bUnexpected first line <$_>\n";
50 print "\b".substr("*+:.", $i % 4, 1) if ($opt_v);
53 print "\bNo!\nntpd did not synchronize.\n" if ($opt_v);