话不多说,直接po代码。
程序代码
- C:\Schedule.bat
at 17:10 /every:M,T,W,Th,F,S,Su cmd /c C:\pingScheme.vbs
pause
- C:\pingScheme.vbs
'引用函数
Const conErr = -2
Function Ping(ByVal Server)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'过程说明:
' 1) 原型 Ping(ByVal 服务器)
' 2) ErrLevel返回值:
'
' |值 |标志 |描述
' -1 True 网络畅通
' 0 False 网络有误
' -2 conErr 网址为空
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim WshShell, intState, Args
If IsEmpty(Server) Then
Ping = conErr : Exit Function
End If
Args = " -n 2"
Set WshShell = WScript.CreateObject("WScript.Shell")
intState = WshShell.run ("ping.exe " & Server & Args, 4, True)
If intState = 0 Then
Ping = True
Else
Ping = False
End If
Set WshShell = Nothing
End Function
Function pingNet(ByVal Server)
Dim WshShell, pingShell
Set WshShell = WScript.CreateObject("WScript.Shell")
Set pingShell = WshShell.exec("cmd /c ping www.baidu.com")
pingNet = pingShell.StdOut.ReadAll()
Set pingShell = Nothing
Set WshShell = Nothing
End Function
Function betweenString(ByVal sourceString, ByVal leftString, ByVal rightString)
dim tempString, tempArray
tempArray = split(sourceString, leftString)
tempString = tempArray(1)
tempArray = split(tempString, rightString)
betweenString = tempArray(0)
End Function
Function cleanOut(ByVal sourceInfo)
dim alreadySentNum, lostNum, averageNum, lostRate
alreadySentNum = betweenString(sourceInfo, "已发送 = ", ",")
lostNum = betweenString(sourceInfo, "丢失 = ", " (")
averageNum = betweenString(sourceInfo, "平均 = ", chr(13))
lostRate = betweenString(sourceInfo, "(", "% 丢失")
cleanOut = array(alreadySentNum, lostNum, averageNum, lostRate&"%")
End Function
dim objFSO,objFile,strline,pingResult,cleanOutResult
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\in.txt", 1)
Currentdate=year(Now)&"-"&Month(Now)&"-"&day(Now)
Set writeObject = objFSO.OpenTextFile("C:\log-"& Currentdate &".csv", 2, true)
writeObject.writeline("IP/host,ping包数,丢包数,平均延时,丢包率")
do until objFile.atendofstream
strline=objFile.readline
pingResult = pingNet(strline)
'msgbox strline & " " & pingResult
cleanOutResult = cleanOut(pingResult)
'msgbox cleanOutResult(0)&" "&cleanOutResult(1)&" "&cleanOutResult(2)&" "&cleanOutResult(3)
writeObject.writeline(strline & "," & cleanOutResult(0) & "," & cleanOutResult(1) & "," & cleanOutResult(2) & "," & cleanOutResult(3))
loop
objFile.close
writeObject.close
set objFSO=nothing
- C:\in.txt
www.baidu.com
blog.csdn.net
Last but not least, 对于自己还在用vbs做脚本,我表示,朕很自豪!
特别记录
一个自作聪明瞎调用导致的bug==
runas /profile /user:administrator "cmd /c start C:\pingScheme.vbs"
pause
简直天真!!
程序结果
测试下来,每天定期备份!
Advance使用手册
使用基本说明
- 注意一定要放在C:\目录下,程序代码这里定死了,因为需求上说随意,我就随意了一下。
- 所有需要ping的条目保存在了这个txt下。
- 需要修改定时脚本运行的时间,可以进入Schedule.bat中进行相应时间的修改。
- 程序正确执行后,将以csv格式做定期保存,在如图所示的c:\目录下。
- 注意一定要以管理员身份运行Schedule.bat!否则,会报有关权限问题的错误。
at命令配置定时器问题
- 以管理员身份运行cmd
- 可在cmd下查看/删除已有的定时任务
- 重复执行之更改了时间的schedule.bat脚本,可能导致定时器过
多,这个时候,可以通过查看/删除相应的定时任务,重新配置任务。
ping条目的输入文件
Schedule.bat时间更改执行