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
|
|
|
|
#version: 2022.12.17
|
2022-12-17 19:08:57 +08:00
|
|
|
|
|
|
|
# 检测本机时间是否准确的脚本
|
|
|
|
|
|
|
|
# 检测系统类型
|
|
|
|
if [ -f /etc/lsb-release ]; then
|
|
|
|
# Ubuntu/Debian/Almalinux
|
|
|
|
OS="Ubuntu/Debian/Almalinux"
|
|
|
|
elif [ -f /etc/redhat-release ]; then
|
|
|
|
# CentOS/Fedora
|
|
|
|
OS="CentOS/Fedora"
|
|
|
|
else
|
|
|
|
# 未知系统
|
|
|
|
OS="Unknown"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# 获取当前时间和网络时间
|
|
|
|
CURRENT_TIME=$(date +%s)
|
|
|
|
NETWORK_TIME=$(wget -qSO- --max-redirect=0 google.com 2>&1 | grep Date: | cut -d' ' -f5-8)
|
|
|
|
NETWORK_TIME_SECONDS=$(date -d "$NETWORK_TIME" +%s)
|
|
|
|
|
|
|
|
# 计算时间差
|
|
|
|
DIFF=$(($NETWORK_TIME_SECONDS-$CURRENT_TIME))
|
|
|
|
|
|
|
|
# 判断时间差是否在允许范围内
|
|
|
|
if [ "$DIFF" -lt 300 ] && [ "$DIFF" -gt -300 ]; then
|
|
|
|
# 在允许范围内,时间准确
|
|
|
|
echo "Time on $OS system is accurate."
|
|
|
|
else
|
|
|
|
# 不在允许范围内,时间不准确,调整时间
|
|
|
|
echo "Time on $OS system is NOT accurate. Adjusting system time to accurate time."
|
|
|
|
if [ "$OS" == "Ubuntu/Debian/Almalinux" ]; then
|
|
|
|
# Ubuntu/Debian/Almalinux 系统使用 ntpdate 命令
|
2022-12-17 19:13:22 +08:00
|
|
|
if [ ! -x "$(command -v ntpdate)" ]; then
|
|
|
|
# ntpdate 命令不存在,安装 ntpdate
|
|
|
|
sudo apt-get update
|
|
|
|
sudo apt-get install ntpdate -y
|
|
|
|
fi
|
2022-12-17 19:19:16 +08:00
|
|
|
sudo ntpdate -u time.nist.gov || sudo ntpdate pool.ntp.org || sudo ntpdate cn.pool.ntp.org
|
2022-12-17 19:08:57 +08:00
|
|
|
elif [ "$OS" == "CentOS/Fedora" ]; then
|
|
|
|
# CentOS/Fedora 系统使用 ntpdate 命令
|
2022-12-17 19:13:22 +08:00
|
|
|
if [ ! -x "$(command -v ntpdate)" ]; then
|
|
|
|
# ntpdate 命令不存在,安装 ntpdate
|
|
|
|
sudo yum update
|
|
|
|
sudo yum install ntpdate -y
|
|
|
|
fi
|
2022-12-17 19:19:16 +08:00
|
|
|
sudo ntpdate time.nist.gov || sudo ntpdate pool.ntp.org || sudo ntpdate cn.pool.ntp.org
|
2022-12-17 19:08:57 +08:00
|
|
|
else
|
|
|
|
# 未知系统
|
|
|
|
echo "Unable to adjust system time on unknown system."
|
|
|
|
fi
|
|
|
|
fi
|
2022-12-17 19:13:22 +08:00
|
|
|
|
2022-12-17 19:29:36 +08:00
|
|
|
# 获取当前时间和网络时间
|
|
|
|
CURRENT_TIME=$(date +%s)
|
|
|
|
NETWORK_TIME=$(wget -qSO- --max-redirect=0 google.com 2>&1 | grep Date: | cut -d' ' -f5-8)
|
|
|
|
NETWORK_TIME_SECONDS=$(date -d "$NETWORK_TIME" +%s)
|
|
|
|
|
|
|
|
# 计算时间差
|
|
|
|
DIFF=$(($NETWORK_TIME_SECONDS-$CURRENT_TIME))
|
|
|
|
|
|
|
|
# 判断时间差是否在允许范围内
|
|
|
|
if [ "$DIFF" -lt 300 ] && [ "$DIFF" -gt -300 ]; then
|
|
|
|
# 在允许范围内,时间准确
|
|
|
|
echo "Time on $OS system is accurate."
|
|
|
|
else
|
|
|
|
# 不在允许范围内,时间不准确
|
|
|
|
echo "Time on $OS system is NOT accurate. Please check your system time and time zone settings."
|
|
|
|
fi
|