1. 首页>
  2. 技术文章>
  3. vbs写一个hta的系统时间同步工具

vbs写一个hta的系统时间同步工具

4/20/23 2:37:51 PM 浏览 1444 评论 0

vbs hta

有一台服务器的时间老是走着走着就慢了10来秒,一直没查出什么原因,云服务商去帮忙去查也查不出原因,我就想写一个简单的程序在windows服务器上不断的去校验,如果有误差,就同步一次时间。同步我们是可以用命令:

w32tm /resync

那么获取网络时间的话,我们可以获取百度header的时间,也可以先在CMD下面使用命令得到网络时间和误差:

w32tm /stripchart /computer:time.windows.com /samples:1 /dataonly

下面的则是获取百度header的时间:

strURL = "http://www.baidu.com"
Set objRequest = CreateObject("MSXML2.XMLHTTP")
' 发送 HEAD 请求,只获取响应头信息
objRequest.open "HEAD", strURL, False
objRequest.send
dteTime = Split(Replace(objRequest.getResponseHeader("Date"), " GMT", ""), ",")(1)
networkTime = DateAdd("h", 8 ,CDate(dteTime))

所有代码如下,保存为.hta文件,双击就可以直接运行:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<hta>
    <style type="text/css">
        * {font-size:14px;font-family:'微软雅黑',Tahoma;margin:0;padding:0;
        }
        table {width:400px;height:400px;}
        .auto-style2 {
            height: 29px;
        }
        .auto-style3 {
            height: 18px;
        }
        .auto-style4 {
            height: 135px;
        }
        .auto-style5 {
            height: 102px;
            vertical-align:top;
            background:black;
            color:white;
            padding:5px;
        }
    </style>
    <title>流行美时间同步工具</title>
    <hta:application
        id="timeSyncApp"
        applicationname="时间同步"
        icon=""
        border="thin"
        showintaskbar="yes"
        scroll="no"
        singleinstance="yes"
        windowState="normal"
    />
    <script language="VBScript">
        ' 获取本地时间和Internet时间
        Sub GetTimes()
            On Error Resume Next
            window.resizeTo 420, 230
            
            strURL = "http://www.baidu.com"
            Set objRequest = CreateObject("MSXML2.XMLHTTP")
            ' 发送 HEAD 请求,只获取响应头信息
            objRequest.open "HEAD", strURL, False
            objRequest.send
            dteTime = Split(Replace(objRequest.getResponseHeader("Date"), " GMT", ""), ",")(1)
            networkTime = DateAdd("h", 8 ,CDate(dteTime))
            
            my_log = "网络时间:" & networkTime & "<br>"
            my_log = my_log & "本机时间:" & now() & "<br>"
            
            diffS = DateDiff("s",networkTime,now)
            my_log = my_log & "误差秒数:" & diffS & "<br>"
            if Math.Abs(diffS) < 5 then
                my_log = my_log & "处理结果:" & "<font color='green'>相差不超过5秒,不需要处理</font>"
            else
                call SyncTime
            end if
           document.all.result.innerHTML = my_log
            set shell = nothing
        End Sub
        ' 同步时间
        Sub SyncTime
            On Error Resume Next
            Set objShell = CreateObject("WScript.Shell")
            cmd = "w32tm /resync"
            ret = objShell.Run(cmd, 0, True)
            If ret <> 0 Then
                document.all.status.innerHTML = "<font color='red'>同步失败,同步时间:" & Now & "</font>"
            Else
                document.all.status.innerHTML = "<font color='green'>同步成功,同步时间:" &Now & "</font>"
            End If
            
            set objShell = nothing
        End Sub
        ' 定期更新时间
        Sub UpdateTimes
            GetTimes
            SetTimeout "UpdateTimes", 5000
        End Sub
        ' 初始化界面
        Sub Init()
            UpdateTimes
        End Sub
        
    </script>
</head>
<body onload="Init" >
    <table class="auto-style4">
        <tr>
            <td class="auto-style2" >获取网络时间结果:</td>
        </tr>
         <tr>
            <td id="result" class="auto-style5"></td>
        </tr>
        <tr>
            <td class="auto-style3"><span id="status"></span></td>
        </tr>
    </table>
</body>
</html>

当然如果用aardio来做的话,也很简单,而且更加灵活,参考:

import console;
import process.popen;
while(true){
var prcs = process.popen("w32tm /stripchart /computer:time.windows.com /samples:1 /dataonly");
var diffS ;
for( all,out,err in prcs.each() ){
  console.log(all)
  
  //最后一行是有个逗号
  if(string.find(all,",")){
    diffS = string.split(all,",")[2]
    diffS = string.replace(diffS,"+","")
    diffS = string.replace(diffS,"s","")
    //转成数字
    var iDiffs = tonumber(diffS)
    
    //if(iDiffs >= 5){
    console.log("误差超过5秒");
    var tb = process.popen("w32tm /resync");
    console.log(tb.readAll());
    //}
    console.log(iDiffs)
  }
     
}
thread.delay(3000);
}


网友讨论