xref: /freebsd/contrib/ntp/scripts/deprecated/ntp-groper (revision 416ba5c74546f32a993436a99516d35008e9f384)
1*2b15cb3dSCy Schubert#!/bin/sh
2*2b15cb3dSCy Schubert#
3*2b15cb3dSCy Schubert# ntpgroper host ...
4*2b15cb3dSCy Schubert#
5*2b15cb3dSCy Schubert# This script checks each hostname given as an argument to see if
6*2b15cb3dSCy Schubert# it is running NTP.  It reports one of the following messages (assume
7*2b15cb3dSCy Schubert# the host is named "dumbo.hp.com":
8*2b15cb3dSCy Schubert#
9*2b15cb3dSCy Schubert#   dumbo.hp.com not registered in DNS
10*2b15cb3dSCy Schubert#   dumbo.hp.com not responding to ping
11*2b15cb3dSCy Schubert#   dumbo.hp.com refused ntpq connection
12*2b15cb3dSCy Schubert#   dumbo.hp.com not responding to NTP
13*2b15cb3dSCy Schubert#   dumbo.hp.com answers NTP version 2, stratum: 3, ref: telford.nsa.hp.com
14*2b15cb3dSCy Schubert#   dumbo.hp.com answers NTP version 3, stratum: 3, ref: telford.nsa.hp.com
15*2b15cb3dSCy Schubert#
16*2b15cb3dSCy Schubert# It ain't pretty, but it is kinda useful.
17*2b15cb3dSCy Schubert#
18*2b15cb3dSCy Schubert# Walter Underwood, 11 Feb 1993, wunder@hpl.hp.com
19*2b15cb3dSCy Schubert#
20*2b15cb3dSCy Schubert# converted to /bin/sh from /bin/ksh by scott@ee.udel.edu 24 Mar 1993
21*2b15cb3dSCy Schubert
22*2b15cb3dSCy SchubertPATH="/usr/local/etc:$PATH" export PATH
23*2b15cb3dSCy Schubert
24*2b15cb3dSCy Schubertverbose=1
25*2b15cb3dSCy Schubertlogfile=/tmp/cntp-log$$
26*2b15cb3dSCy Schubertntpqlog=/tmp/cntp-ntpq$$
27*2b15cb3dSCy Schubert
28*2b15cb3dSCy Schubert# I wrap the whole thing in parens so that it is possible to redirect
29*2b15cb3dSCy Schubert# all the output somewhere, if desired.
30*2b15cb3dSCy Schubert(
31*2b15cb3dSCy Schubertfor host in $*
32*2b15cb3dSCy Schubertdo
33*2b15cb3dSCy Schubert    # echo "Trying $host."
34*2b15cb3dSCy Schubert
35*2b15cb3dSCy Schubert    gethost $host > /dev/null 2>&1
36*2b15cb3dSCy Schubert    if [ $? -ne 0 ]
37*2b15cb3dSCy Schubert    then
38*2b15cb3dSCy Schubert        echo "$host not registered in DNS"
39*2b15cb3dSCy Schubert        continue
40*2b15cb3dSCy Schubert    fi
41*2b15cb3dSCy Schubert
42*2b15cb3dSCy Schubert    ping $host 64 1 > /dev/null 2>&1
43*2b15cb3dSCy Schubert    if [ $? -ne 0 ]
44*2b15cb3dSCy Schubert    then
45*2b15cb3dSCy Schubert        echo "$host not responding to ping"
46*2b15cb3dSCy Schubert	continue
47*2b15cb3dSCy Schubert    fi
48*2b15cb3dSCy Schubert
49*2b15cb3dSCy Schubert    # Attempt to contact with version 3 ntp, then try version 2.
50*2b15cb3dSCy Schubert    for version in 3 2
51*2b15cb3dSCy Schubert    do
52*2b15cb3dSCy Schubert
53*2b15cb3dSCy Schubert        ntpq -c "ntpversion $version" -p $host > $ntpqlog 2>&1
54*2b15cb3dSCy Schubert
55*2b15cb3dSCy Schubert        if fgrep -s 'Connection refused' $ntpqlog
56*2b15cb3dSCy Schubert        then
57*2b15cb3dSCy Schubert            echo "$host refused ntpq connection"
58*2b15cb3dSCy Schubert            break
59*2b15cb3dSCy Schubert        fi
60*2b15cb3dSCy Schubert
61*2b15cb3dSCy Schubert        responding=1
62*2b15cb3dSCy Schubert        fgrep -s 'timed out, nothing received' $ntpqlog > /dev/null && responding=0
63*2b15cb3dSCy Schubert
64*2b15cb3dSCy Schubert        if   [ $responding -eq 1 ]
65*2b15cb3dSCy Schubert        then
66*2b15cb3dSCy Schubert	    ntpq -c "ntpversion $version" -c rl $host > $ntpqlog
67*2b15cb3dSCy Schubert
68*2b15cb3dSCy Schubert            # First we extract the reference ID (usually a host or a clock)
69*2b15cb3dSCy Schubert            synchost=`fgrep "refid=" $ntpqlog`
70*2b15cb3dSCy Schubert            #synchost=${synchost##*refid=} # strip off the beginning of the line
71*2b15cb3dSCy Schubert            #synchost=${synchost%%,*}      # strip off the end
72*2b15cb3dSCy Schubert	    synchost=`expr "$synchost" : '.*refid=\([^,]*\),.*'`
73*2b15cb3dSCy Schubert
74*2b15cb3dSCy Schubert            # Next, we get the stratum
75*2b15cb3dSCy Schubert            stratum=`fgrep "stratum=" $ntpqlog`
76*2b15cb3dSCy Schubert            #stratum=${stratum##*stratum=}
77*2b15cb3dSCy Schubert            #stratum=${stratum%%,*}
78*2b15cb3dSCy Schubert	    stratum=`expr "$stratum" : '.*stratum=\([^,]*\),.*'`
79*2b15cb3dSCy Schubert
80*2b15cb3dSCy Schubert	    echo "$host answers NTP version $version, stratum: $stratum, ref: $synchost"
81*2b15cb3dSCy Schubert            break;
82*2b15cb3dSCy Schubert        fi
83*2b15cb3dSCy Schubert
84*2b15cb3dSCy Schubert	if [ $version -eq 2 -a $responding -eq 0 ]
85*2b15cb3dSCy Schubert        then
86*2b15cb3dSCy Schubert            echo "$host not responding to NTP"
87*2b15cb3dSCy Schubert        fi
88*2b15cb3dSCy Schubert    done
89*2b15cb3dSCy Schubertdone
90*2b15cb3dSCy Schubert)
91*2b15cb3dSCy Schubert# ) >> $logfile
92*2b15cb3dSCy Schubert
93*2b15cb3dSCy Schubertif [ -f $ntpqlog ]; then
94*2b15cb3dSCy Schubert    rm $ntpqlog
95*2b15cb3dSCy Schubertfi
96