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