3 # John Hay -- John.Hay@icomtek.csir.co.za / jhay@FreeBSD.org
14 $dodns = 0 if (defined($opt_n));
17 $host ||= "127.0.0.1";
21 $cmd = "$ntpq -n -c rv $host";
22 open(PH, $cmd . "|") || die "failed to start command $cmd: $!";
24 $stratum = $1 if (/stratum=(\d+)/);
25 $peer = $1 if (/peer=(\d+)/);
26 # Very old servers report phase and not offset.
27 $offset = $1 if (/(?:offset|phase)=([^\s,]+)/);
28 $rootdelay = $1 if (/rootdelay=([^\s,]+)/);
29 $refid = $1 if (/refid=([^\s,]+)/);
31 close(PH) || die "$cmd failed";
32 last if ($stratum == 255);
36 # Only do lookups of IPv4 addresses. The standard lookup functions
37 # of perl only do IPv4 and I don't know if we should require extras.
38 if ($dodns && $host =~ /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/) {
39 $iaddr = inet_aton($host);
40 $name = (gethostbyaddr($iaddr, AF_INET))[0];
41 $dhost = $name if (defined($name));
43 printf("%s: stratum %d, offset %f, root distance %f",
44 $dhost, $stratum, $offset, $rootdelay);
45 printf(", refid '%s'", $refid) if ($stratum == 1);
47 last if ($stratum == 0 || $stratum == 1 || $stratum == 16);
48 last if ($refid =~ /^127\.127\.\d{1,3}\.\d{1,3}$/);
50 $cmd = "$ntpq -n -c \"pstat $peer\" $host";
51 open(PH, $cmd . "|") || die "failed to start command $cmd: $!";
54 $thost = $1, last if (/srcadr=(\S+),/);
56 close(PH) || die "$cmd failed";
57 last if ($thost eq "");