-
Notifications
You must be signed in to change notification settings - Fork 1
/
ntp-configure.sh
executable file
·239 lines (185 loc) · 6.21 KB
/
ntp-configure.sh
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#! /usr/bin/env bash
# MIT License
# Copyright (c) 2023 Mehmet Emin BAŞOĞLU
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# set -e
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
BOLD='\033[1m'
# Emojis
CHECK='\U2705'
GRIMACING_FACE='\U1F62C'
usage() {
echo "Usage: $0 [-h] [--help] <command> [args]"
echo
echo "Commands:"
echo -e "\t server-configure"
echo -e "\t client-configure"
echo
echo
echo "The \"ntp\" package in Ubuntu comes with default Ubunu NTP pools."
echo "To create a server with default NTP pools:"
echo
echo -e "${BOLD}$ $0 server-configure${NC}"
echo
echo
echo "https://ntppool.org offers NTP servers all around the world."
echo "You can find a list of servers in Turkey here: https://www.pool.ntp.org/zone/tr"
echo "To create a server with custom remote NTP servers add \"--remote-ntp-servers\" "
echo "optional parameter and enter your addresses in \"\" with space seperated."
echo
echo -e "${BOLD}$ $0 server-configure --remote-ntp-servers \"2.tr.pool.ntp.org 0.europe.pool.ntp.org 3.europe.pool.ntp.org\"${NC}"
echo
echo
echo "To configure a client, you need the address of the server:"
echo
echo -e "${BOLD}$ $0 client-configure 172.17.0.3${NC}"
}
invalid_args() {
usage
echo -e "${RED}Invalid argument.${NC} ${GRIMACING_FACE}"
}
check_sudo() {
if hash sudo 2>/dev/null; then
echo "Found sudo."
SUDO=sudo
else
echo "Couldn't find sudo."
SUDO=""
fi
}
install-packages() {
dpkg -l | grep ntp >/dev/null
EXIT_CODE=$?
if [ ${EXIT_CODE} -eq 0 ]; then
echo -e "${BLUE}Ntp is already installed.${NC}"
return 0
fi
echo -e "${BLUE}Installing ntp.${NC}"
${SUDO} apt-get update
# ${SUDO} apt-get upgrade -y
${SUDO} apt-get install -y ntp
echo -e "${GREEN}Installed ntp.${NC}"
}
disable_timesyncd() {
if hash "timedatectl" 2>/dev/null; then
# Found command.
# We need to disable systemd-timesyncd because we use ntpd.
${SUDO} timedatectl set-ntp no
echo -e "${GREEN}Disabled timesyncd.${NC}"
fi
}
server-configure() {
install-packages
disable_timesyncd
if [ -n "$1" ]; then
echo bura
# User specified remote servers.
# Parse remote servers.
IFS=' ' read -a ntp_servers <<<"$1"
# Comment out default NTP pools.
${SUDO} sed 's/^pool/#&/' /etc/ntp.conf -i
# Add NTP servers.
for f in "${ntp_servers[@]}"; do
# Server may already be in the config, check it.
grep "$f" /etc/ntp.conf >/dev/null
EXIT_CODE=$?
if [ ${EXIT_CODE} -eq 0 ]; then
echo -e "${BLUE}$f already exists in config file.${NC}"
continue
fi
printf 'server %s iburst' "$f" | ${SUDO} tee -a /etc/ntp.conf
echo | ${SUDO} tee -a /etc/ntp.conf
done
echo -e "${GREEN}Configured remote servers.${NC}"
fi
# In case of losing Internet connectivity, use local clock as default.
grep "server 127.127.1.0" /etc/ntp.conf >/dev/null
EXIT_CODE=$?
if [ ${EXIT_CODE} -eq 1 ]; then
echo "server 127.127.1.0" | ${SUDO} tee -a /etc/ntp.conf
fi
grep "fudge 127.127.1.0 stratum 10" /etc/ntp.conf >/dev/null
EXIT_CODE=$?
if [ ${EXIT_CODE} -eq 1 ]; then
echo "fudge 127.127.1.0 stratum 10" | ${SUDO} tee -a /etc/ntp.conf
fi
echo -e "${GREEN}Set local clock as default.${NC}"
# Restart NTP deamon.
${SUDO} service ntp restart
echo -e "${BLUE}Waiting for some time to communicate with peers.${NC}"
# Wait for some time to sync peers.
sleep 5
# Verbose NTP peers.
ntpq -p
echo
echo -e "${CHECK}${GREEN} Server is configured.${NC}"
}
client-configure() {
install-packages
disable_timesyncd
# Comment out default NTP pools.
${SUDO} sed 's/^pool/#&/' /etc/ntp.conf -i
grep "server $1 iburst" /etc/ntp.conf >/dev/null
EXIT_CODE=$?
if [ ${EXIT_CODE} -eq 1 ]; then
# Add local NTP server.
echo "server $1 iburst" | ${SUDO} tee -a /etc/ntp.conf
fi
echo -e "${GREEN}Set local NTP server.${NC}"
# Restart NTP deamon.
${SUDO} service ntp restart
echo -e "${BLUE}Waiting for some time to communicate with peers.${NC}"
# Wait for some time to sync peers.
sleep 5
# Verbose NTP peers.
ntpq -p
echo
echo -e "${CHECK}${GREEN} Client is configured.${NC}"
}
main() {
check_sudo
if [ "$1" = server-configure ]; then
# Optional argument.
if [ "$2" = "--remote-ntp-servers" ]; then
# User entered remote servers.
server-configure "$3"
elif [ "$2" = "" ]; then
# No remote servers provided, use defaults.
server-configure
else
invalid_args
fi
elif [ "$1" = client-configure ]; then
if [ "$2" = "" ]; then
invalid_args
else
client-configure "$2"
fi
elif [ "$1" = "--help" ]; then
usage
elif [ "$1" = "-h" ]; then
usage
else
# Print usage if no parameter is provided.
invalid_args
fi
}
main "$@"