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