one-click-installation-script/modify_time.sh

117 lines
4.1 KiB
Bash
Raw Normal View History

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-17 22:02:19 +08:00
# 检测本机时间是否准确,如果不准确则修复的脚本
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() {
# 支持系统Ubuntu 12+Debian 6+
ver="2022.12.17"
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+FedoraAlmalinux 8.5+"
2022-12-17 22:24:11 +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(){
# 获取当前时间和网络时间
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
# 在允许范围内,时间准确
2022-12-18 11:50:09 +08:00
green "Time on $OS system is accurate."
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
}
check_again(){
# 获取当前时间和网络时间
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
# 在允许范围内,时间准确
2022-12-18 11:50:09 +08:00
green "Time on $OS system is accurate."
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 19:29:36 +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