脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|shell|

服务器之家 - 脚本之家 - shell - Shell实现系统时间和BIOS时间同步校准脚本分享

Shell实现系统时间和BIOS时间同步校准脚本分享

2023-02-22 13:54shell教程网 shell

这篇文章主要介绍了Shell实现系统时间和BIOS时间同步校准脚本分享,本文给出了多个时间同步服务器,然后用ntpdate轮询同步,需要的朋友可以参考下

该脚本主要是从定义好的NTP服务器列表获取服务器地址进行同步,如果第一个不成功,会继续换下一个地址进行同步!不完善之处还请指出!

 

复制代码 代码如下:


#!/bin/bash
# NTP网络时间校正脚本
# 奔跑

 

#NTP服务器数组列表
ntpServer=(

[0]=ntp.fudan.edu.cn
[1]=asia.pool.ntp.org
[2]=210.72.145.44
[3]=133.100.11.8
[4]=ntp.sjtu.edu.cn
[5]=time.scau.edu.cn

)

#校验#
serverNum=`echo ${#ntpServer[*]}`
NUM=0
for (( i=0; i<=$serverNum; i++ )); do
  
    echo -n "正在和NTP服务器${ntpServer[$NUM]}校验中..."
    /usr/sbin/ntpdate ${ntpServer[$NUM]} >> /dev/null 2>&1
    if [ $? -eq 0 ]; then
        echo -e "\e[1;32m\t\t\t\t\t[成功]\e[0m"
        break
    else
        echo -e "\e[1;31m\t\t\t\t\t[失败]\e[0m"
        let NUM++
    fi
    sleep 2

done

#设置BIOS时间和系统时间一致
hwclock --systohc

 

延伸 · 阅读

精彩推荐