summaryrefslogtreecommitdiff
path: root/net-misc/clockspeed/files/ntpclockset
blob: c4c9d1b68ccb23b0efe03dcc3e779e0a98cb9ed0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/bin/bash

# Updates by Sascha Silbe

usage() {
	cat << EOF
Usage: ntpclockset [options] <NTP server>
Example: ntpclockset -q 192.168.0.1

Options:
	--quiet (-q)	be quiet (only show errors)
	--help  (-h)	show this text and exit
EOF
	exit 1
}

quiet=0

for curArg in "$@" ; do
	case "${curArg}" in
	-q|--quiet) quiet=1;;
	-h|--help) 	usage;;
	-*)			echo "Invalid option '${curArg}'"
				usage;;
	*)			NTPSERVER="${curArg}";;
	esac
done

[ -z $NTPSERVER ] && NTPSERVER=`dnsip pool.ntp.org | awk '{print $1}'`

tmpfile="`mktemp`"
# display how much your clock is off by
if ! sntpclock $NTPSERVER > ${tmpfile} ; then
	echo "!!! Could not contact NTP server: $NTPSERVER" >&2
	exit 2
fi

if [ ${quiet} -eq 0 ] ; then
	echo ">>> Current clock sync:"
	cat ${tmpfile} | clockview
	echo
	echo -n ">>> Now setting clock ..."
fi
cat ${tmpfile} | clockadd
if [ ${quiet} -eq 0 ] ; then
	echo "   [ok]"
	echo -n ">>> Writing time to hardware clock ..."
fi
/sbin/hwclock --systohc
if [ ${quiet} -eq 0 ] ; then
	echo "   [ok]"
	echo
	echo ">>> New clock sync:"
	sntpclock $NTPSERVER | clockview
fi
rm -f ${tmpfile}