ntpclockset 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #!/bin/bash
  2. # Updates by Sascha Silbe
  3. usage() {
  4. cat << EOF
  5. Usage: ntpclockset [options] <NTP server>
  6. Example: ntpclockset -q 192.168.0.1
  7. Options:
  8. --quiet (-q) be quiet (only show errors)
  9. --help (-h) show this text and exit
  10. EOF
  11. exit 1
  12. }
  13. quiet=0
  14. for curArg in "$@" ; do
  15. case "${curArg}" in
  16. -q|--quiet) quiet=1;;
  17. -h|--help) usage;;
  18. -*) echo "Invalid option '${curArg}'"
  19. usage;;
  20. *) NTPSERVER="${curArg}";;
  21. esac
  22. done
  23. [ -z $NTPSERVER ] && NTPSERVER=`dnsip pool.ntp.org | awk '{print $1}'`
  24. tmpfile="`mktemp`"
  25. # display how much your clock is off by
  26. if ! sntpclock $NTPSERVER > ${tmpfile} ; then
  27. echo "!!! Could not contact NTP server: $NTPSERVER" >&2
  28. exit 2
  29. fi
  30. if [ ${quiet} -eq 0 ] ; then
  31. echo ">>> Current clock sync:"
  32. cat ${tmpfile} | clockview
  33. echo
  34. echo -n ">>> Now setting clock ..."
  35. fi
  36. cat ${tmpfile} | clockadd
  37. if [ ${quiet} -eq 0 ] ; then
  38. echo " [ok]"
  39. echo -n ">>> Writing time to hardware clock ..."
  40. fi
  41. /sbin/hwclock --systohc
  42. if [ ${quiet} -eq 0 ] ; then
  43. echo " [ok]"
  44. echo
  45. echo ">>> New clock sync:"
  46. sntpclock $NTPSERVER | clockview
  47. fi
  48. rm -f ${tmpfile}