/contrib/ntp/scripts/ntp-groper

https://bitbucket.org/freebsd/freebsd-head/ · Shell · 95 lines · 49 code · 15 blank · 31 comment · 9 complexity · d3734cd7969a34fbb4313719c90428cb MD5 · raw file

  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. PATH="/usr/local/etc:$PATH" export PATH
  22. verbose=1
  23. logfile=/tmp/cntp-log$$
  24. ntpqlog=/tmp/cntp-ntpq$$
  25. # I wrap the whole thing in parens so that it is possible to redirect
  26. # all the output somewhere, if desired.
  27. (
  28. for host in $*
  29. do
  30. # echo "Trying $host."
  31. gethost $host > /dev/null 2>&1
  32. if [ $? -ne 0 ]
  33. then
  34. echo "$host not registered in DNS"
  35. continue
  36. fi
  37. ping $host 64 1 > /dev/null 2>&1
  38. if [ $? -ne 0 ]
  39. then
  40. echo "$host not responding to ping"
  41. continue
  42. fi
  43. # Attempt to contact with version 3 ntp, then try version 2.
  44. for version in 3 2
  45. do
  46. ntpq -c "ntpversion $version" -p $host > $ntpqlog 2>&1
  47. if fgrep -s 'Connection refused' $ntpqlog
  48. then
  49. echo "$host refused ntpq connection"
  50. break
  51. fi
  52. responding=1
  53. fgrep -s 'timed out, nothing received' $ntpqlog > /dev/null && responding=0
  54. if [ $responding -eq 1 ]
  55. then
  56. ntpq -c "ntpversion $version" -c rl $host > $ntpqlog
  57. # First we extract the reference ID (usually a host or a clock)
  58. synchost=`fgrep "refid=" $ntpqlog`
  59. #synchost=${synchost##*refid=} # strip off the beginning of the line
  60. #synchost=${synchost%%,*} # strip off the end
  61. synchost=`expr "$synchost" : '.*refid=\([^,]*\),.*'`
  62. # Next, we get the stratum
  63. stratum=`fgrep "stratum=" $ntpqlog`
  64. #stratum=${stratum##*stratum=}
  65. #stratum=${stratum%%,*}
  66. stratum=`expr "$stratum" : '.*stratum=\([^,]*\),.*'`
  67. echo "$host answers NTP version $version, stratum: $stratum, ref: $synchost"
  68. break;
  69. fi
  70. if [ $version -eq 2 -a $responding -eq 0 ]
  71. then
  72. echo "$host not responding to NTP"
  73. fi
  74. done
  75. done
  76. )
  77. # ) >> $logfile
  78. if [ -f $ntpqlog ]; then
  79. rm $ntpqlog
  80. fi