2022-12-17 19:08:57 +08:00
|
|
|
|
#!/bin/bash
|
2022-12-17 19:14:09 +08:00
|
|
|
|
#by spiritlhl
|
|
|
|
|
#from https://github.com/spiritLHLS/one-click-installation-script
|
2022-12-18 11:57:18 +08:00
|
|
|
|
#version: 2022.12.18
|
2022-12-17 19:08:57 +08:00
|
|
|
|
|
2022-12-18 11:50:09 +08:00
|
|
|
|
red(){ echo -e "\033[31m\033[01m$1$2\033[0m"; }
|
|
|
|
|
green(){ echo -e "\033[32m\033[01m$1$2\033[0m"; }
|
|
|
|
|
yellow(){ echo -e "\033[33m\033[01m$1$2\033[0m"; }
|
|
|
|
|
reading(){ read -rp "$(green "$1")" "$2"; }
|
|
|
|
|
|
2022-12-17 22:22:53 +08:00
|
|
|
|
head() {
|
2022-12-19 18:25:03 +08:00
|
|
|
|
ver="2022.12.18"
|
2022-12-17 22:24:11 +08:00
|
|
|
|
changeLog="一键修复本机系统时间"
|
2022-12-17 22:22:53 +08:00
|
|
|
|
clear
|
|
|
|
|
echo "#######################################################################"
|
2022-12-17 22:30:03 +08:00
|
|
|
|
echo "# ${YELLOW}一键修复本机系统时间脚本${PLAIN} #"
|
2022-12-17 22:22:53 +08:00
|
|
|
|
echo "# 版本:$ver #"
|
2022-12-17 22:30:03 +08:00
|
|
|
|
echo "# 更新日志:$changeLog #"
|
2022-12-17 22:22:53 +08:00
|
|
|
|
echo "# ${GREEN}作者${PLAIN}: spiritlhl #"
|
|
|
|
|
echo "# ${GREEN}作仓库${PLAIN}: https://github.com/spiritLHLS/one-click-installation-script #"
|
|
|
|
|
echo "#######################################################################"
|
|
|
|
|
echo "支持系统:Ubuntu 18+,Debian 8+,centos 7+,Fedora,Almalinux 8.5+"
|
2022-12-18 12:54:50 +08:00
|
|
|
|
echo "检测修复本机系统时间,对应时区时间,如果相差超过300秒的合理范围则校准时间"
|
2022-12-17 22:22:53 +08:00
|
|
|
|
# Display prompt asking whether to proceed with checking and changing
|
2022-12-18 11:50:09 +08:00
|
|
|
|
reading "Do you want to proceed with checking and changing? [y/n] " confirm
|
2022-12-17 22:22:53 +08:00
|
|
|
|
echo ""
|
2022-12-17 19:08:57 +08:00
|
|
|
|
|
2022-12-17 22:22:53 +08:00
|
|
|
|
# Check user's input and exit if they do not want to proceed
|
|
|
|
|
if [ "$confirm" != "y" ]; then
|
|
|
|
|
exit 0
|
|
|
|
|
fi
|
|
|
|
|
}
|
2022-12-17 19:08:57 +08:00
|
|
|
|
|
|
|
|
|
|
2022-12-17 22:22:53 +08:00
|
|
|
|
check_os() {
|
|
|
|
|
# 检测系统类型
|
|
|
|
|
if [ -f /etc/lsb-release ]; then
|
|
|
|
|
# Ubuntu/Debian/Almalinux
|
|
|
|
|
OS="Ubuntu/Debian/Almalinux"
|
|
|
|
|
elif [ -f /etc/redhat-release ]; then
|
|
|
|
|
# CentOS/Fedora
|
|
|
|
|
OS="CentOS/Fedora"
|
2022-12-17 19:08:57 +08:00
|
|
|
|
else
|
|
|
|
|
# 未知系统
|
2022-12-17 22:22:53 +08:00
|
|
|
|
OS="Unknown"
|
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
main(){
|
2022-12-18 12:54:20 +08:00
|
|
|
|
# 获取当前时区信息
|
|
|
|
|
TIMEZONE=$(date +%z)
|
2022-12-17 22:22:53 +08:00
|
|
|
|
# 获取当前时间和网络时间
|
2022-12-18 12:47:34 +08:00
|
|
|
|
CURRENT_TIME=$(date -u +%s)
|
2022-12-17 22:22:53 +08:00
|
|
|
|
NETWORK_TIME=$(wget -qSO- --max-redirect=0 google.com 2>&1 | grep Date: | cut -d' ' -f5-8)
|
2022-12-18 12:47:34 +08:00
|
|
|
|
NETWORK_TIME_SECONDS=$(TZ=":UTC" date -d "$NETWORK_TIME" +%s)
|
2022-12-17 22:22:53 +08:00
|
|
|
|
|
|
|
|
|
# 计算时间差
|
|
|
|
|
DIFF=$(($NETWORK_TIME_SECONDS-$CURRENT_TIME))
|
|
|
|
|
|
2022-12-18 12:54:20 +08:00
|
|
|
|
# 根据时区信息增加或减少时间差的允许范围
|
2022-12-24 22:25:48 +08:00
|
|
|
|
ALLOWED_DIFF=$((300 + "${TIMEZONE:0:3}" * 3600 + "${TIMEZONE:3:2}" * 60))
|
|
|
|
|
|
2022-12-18 12:54:20 +08:00
|
|
|
|
|
2022-12-17 22:22:53 +08:00
|
|
|
|
# 判断时间差是否在允许范围内
|
2022-12-18 12:54:20 +08:00
|
|
|
|
if [ "$DIFF" -lt "$ALLOWED_DIFF" ] && [ "$DIFF" -gt "-$ALLOWED_DIFF" ]; then
|
2022-12-17 22:22:53 +08:00
|
|
|
|
# 在允许范围内,时间准确
|
2022-12-18 11:50:09 +08:00
|
|
|
|
green "Time on $OS system is accurate."
|
2022-12-18 13:09:32 +08:00
|
|
|
|
echo "Current time: $(date)"
|
2022-12-18 12:54:20 +08:00
|
|
|
|
exit 0
|
2022-12-17 22:22:53 +08:00
|
|
|
|
else
|
|
|
|
|
# 不在允许范围内,时间不准确,调整时间
|
2022-12-18 11:50:09 +08:00
|
|
|
|
yellow "Time on $OS system is NOT accurate. Adjusting system time to accurate time."
|
2022-12-17 22:22:53 +08:00
|
|
|
|
if [ "$OS" == "Ubuntu/Debian/Almalinux" ]; then
|
|
|
|
|
# Ubuntu/Debian/Almalinux 系统使用 ntpdate 命令
|
|
|
|
|
if [ ! -x "$(command -v ntpdate)" ]; then
|
|
|
|
|
# ntpdate 命令不存在,安装 ntpdate
|
|
|
|
|
sudo apt-get update
|
|
|
|
|
sudo apt-get install ntpdate -y
|
|
|
|
|
fi
|
|
|
|
|
sudo ntpdate -u time.nist.gov || sudo ntpdate pool.ntp.org || sudo ntpdate cn.pool.ntp.org
|
|
|
|
|
elif [ "$OS" == "CentOS/Fedora" ]; then
|
|
|
|
|
# CentOS/Fedora 系统使用 ntpdate 命令
|
|
|
|
|
if [ ! -x "$(command -v ntpdate)" ]; then
|
|
|
|
|
# ntpdate 命令不存在,安装 ntpdate
|
|
|
|
|
sudo yum update
|
|
|
|
|
sudo yum install ntpdate -y
|
|
|
|
|
fi
|
|
|
|
|
sudo ntpdate time.nist.gov || sudo ntpdate pool.ntp.org || sudo ntpdate cn.pool.ntp.org
|
|
|
|
|
else
|
|
|
|
|
# 未知系统
|
2022-12-18 11:50:09 +08:00
|
|
|
|
red "Unable to adjust system time on unknown system."
|
2022-12-17 22:22:53 +08:00
|
|
|
|
fi
|
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-18 12:47:34 +08:00
|
|
|
|
|
2022-12-17 22:22:53 +08:00
|
|
|
|
check_again(){
|
|
|
|
|
# 获取当前时间和网络时间
|
2022-12-18 12:54:20 +08:00
|
|
|
|
CURRENT_TIME=$(date -u +%s)
|
2022-12-17 22:22:53 +08:00
|
|
|
|
NETWORK_TIME=$(wget -qSO- --max-redirect=0 google.com 2>&1 | grep Date: | cut -d' ' -f5-8)
|
2022-12-18 12:54:20 +08:00
|
|
|
|
NETWORK_TIME_SECONDS=$(TZ=":UTC" date -d "$NETWORK_TIME" +%s)
|
2022-12-17 22:22:53 +08:00
|
|
|
|
|
2022-12-18 12:54:20 +08:00
|
|
|
|
# 获取当前时区信息
|
|
|
|
|
TIMEZONE=$(date +%z)
|
|
|
|
|
# 获取网络时间对应的时区信息
|
2022-12-18 12:47:34 +08:00
|
|
|
|
NETWORK_TZ=$(echo "$NETWORK_TIME" | awk '{print $5}')
|
|
|
|
|
# 计算时区差,单位是秒
|
2022-12-18 12:54:20 +08:00
|
|
|
|
TZ_DIFF=$((($(TZ=":$NETWORK_TZ" date -d "now" +%s)-$(TZ=":$TIMEZONE" date -d "now" +%s))/3600*3600))
|
2022-12-18 12:47:34 +08:00
|
|
|
|
|
2022-12-17 22:22:53 +08:00
|
|
|
|
# 计算时间差
|
2022-12-18 12:47:34 +08:00
|
|
|
|
DIFF=$(($NETWORK_TIME_SECONDS-$CURRENT_TIME-$TZ_DIFF))
|
2022-12-17 22:22:53 +08:00
|
|
|
|
|
2022-12-18 12:54:20 +08:00
|
|
|
|
# 根据时区信息增加或减少时间差的允许范围
|
2022-12-24 22:25:48 +08:00
|
|
|
|
ALLOWED_DIFF=$((300 + "${TIMEZONE:0:3}" * 3600 + "${TIMEZONE:3:2}" * 60))
|
|
|
|
|
|
2022-12-18 12:54:20 +08:00
|
|
|
|
|
2022-12-17 22:22:53 +08:00
|
|
|
|
# 判断时间差是否在允许范围内
|
2022-12-18 12:54:20 +08:00
|
|
|
|
if [ "$DIFF" -lt "$ALLOWED_DIFF" ] && [ "$DIFF" -gt "-$ALLOWED_DIFF" ]; then
|
2022-12-17 22:22:53 +08:00
|
|
|
|
# 在允许范围内,时间准确
|
2022-12-18 11:50:09 +08:00
|
|
|
|
green "Time on $OS system is accurate."
|
2022-12-18 13:09:32 +08:00
|
|
|
|
echo "Current time: $(date)"
|
2022-12-17 22:22:53 +08:00
|
|
|
|
else
|
|
|
|
|
# 不在允许范围内,时间不准确
|
2022-12-18 11:50:09 +08:00
|
|
|
|
red "Time on $OS system is NOT accurate. Please check your system time and time zone settings again!"
|
2022-12-17 19:08:57 +08:00
|
|
|
|
fi
|
2022-12-17 22:22:53 +08:00
|
|
|
|
}
|
2022-12-17 19:13:22 +08:00
|
|
|
|
|
2022-12-17 22:22:53 +08:00
|
|
|
|
head
|
|
|
|
|
check_os
|
|
|
|
|
main
|
2022-12-17 22:31:05 +08:00
|
|
|
|
sleep 1
|
2022-12-17 22:22:53 +08:00
|
|
|
|
check_again
|