65.9K
CodeProject 正在变化。 阅读更多。
Home

使用 VBScript 按最佳响应时间对服务器进行排序 - 使用 XP 和 2000 的自定义 ping 函数

starIconstarIconstarIconstarIconemptyStarIcon

4.00/5 (4投票s)

2007年12月14日

CPOL
viewsIcon

33231

downloadIcon

233

使用一个自定义 ping 函数,该函数适用于 XP 和 2000。始终选择响应时间最佳的服务器。

引言

在之前编写一个脚本时,需要根据平均响应时间对服务器列表进行排序。能够按响应时间对服务器进行排序,使您能够确保连接到最佳服务器(基于您的位置),并避免服务器宕机。脚本中包含两个函数:一个自定义 ping 函数,专为 XP 和 2000 设计,以及一个 ServersByPingTime 函数,它接收一个服务器列表并返回一个基于最佳平均响应时间的数组。以下是脚本和示例输出。您还可以通过单击页面顶部的链接下载示例。

代码

Dim arr : arr = Array( "GOOGLE.COM" , "YAHOO.COM" , _
                       "CAT" , "LOCALHOST")
Dim out
Call ServersByPingTime( arr , out , True )
Dim s
WScript.Echo "In order fastest to slowest: "
For Each S in out
 WScript.Echo s
Next 

' Ping function will work on Windows 2000 and Windows XP
' without using the Win32_PingStatus
Function Ping(strHost , ByRef bytesSent , ByRef bytesReceived , _
         ByRef bytesLost , ByRef minMs , ByRef maxMs , ByRef aveMs )
 Ping = False
 Dim objShell, objExec, strPingResults, bRet
 Set objShell = CreateObject("WScript.Shell")
 Set objExec = objShell.Exec("ping -n 1 " & strHost) 
 Do
     WScript.Sleep 100
 Loop Until objExec.Status <> 0
 strPingResults = objExec.StdOut.ReadAll
 Dim regexpingstats : Set regexpingstats = new regexp
  regexpingstats.Pattern = "Packets:\s+Sent\s+=\s+([0-9]+).*Received" & _ 
                           "\s+=\s+([0-9]+).*Lost\s+=\s+([0-9]+)(?:.*\s)+" & _ 
                           "Minimum\s+=\s+([0-9]+)ms.*Maximum\s+=\s+" & _ 
                           "([0-9]+)ms.*Average\s+=\s+([0-9]+)ms"
 regexpingstats.Global = True
 regexpingstats.IgnoreCase = True
 regexpingstats.MultiLine = True
 If regexpingstats.Test(strPingResults) Then
  Dim m : Set m = regexpingstats.Execute(strPingResults)
  bytesSent = CInt(m.Item(0).subMatches.Item(0))
  bytesReceived = CInt(m.Item(0).subMatches.Item(1))
  bytesLost = CInt(m.Item(0).subMatches.Item(2))
  minMs = CInt(m.Item(0).subMatches.Item(3))
  maxMs = CInt(m.Item(0).subMatches.Item(4))
  aveMs = CInt(m.Item(0).subMatches.Item(5))
  Ping = Eval( bytesSent > bytesLost )
 End If
End Function

'Returns false if no server were found alive
'outSortedByMs - array sorted fastest response to slowest response time
Public Function ServersByPingTime( ByVal inSeverList , _
                ByRef outSortedByMs , bVerbose )
  On Error Resume Next
  ServersByPingTime = False
  outLivingSorted = Array
  Dim s, i , j , temp
  If bVerbose Then
    WScript.Echo("[Performing Connectivity Test of Defined Servers]")
  For Each s In inSeverList 
   If bVerbose Then wscript.StdOut.Write("        Server: " & s )
   Dim bs, br, bl, mi , ma , av
   If Ping( s , bs, br, bl, mi , ma , av ) Then
    If bVerbose Then
     WScript.Echo(" [Passed]")
     WScript.Echo("    Bytes Sent: " & bs )
     WScript.Echo("    Bytes Recv: " & br )
     WScript.Echo("    Bytes Lost: " & bl )
     WScript.Echo("        Min ms: " & mi )
     WScript.Echo("        Max ms: " & ma )
     WScript.Echo("    Average ms: " & av )
     WScript.Echo("")
    End If 
    i = UBound(outLivingSorted) + 1  
    ReDim Preserve outLivingSorted(i)
    outLivingSorted(i) = Array(s,av)
    ServersByPingTime = True ' Success there are servers alive... 
   Else 
    If bVerbose Then 
     WScript.Echo(" [Failed]")
     WScript.Echo("")
    End if
   End If 
  Next
  'Sort...
  For i = UBound(outLivingSorted) - 1 To 0 Step -1
    For j = 0 To i
      If outLivingSorted(j)(1) > outLivingSorted(j+1)(1) Then
         temp=outLivingSorted(j+1)
         outLivingSorted(j+1)=outLivingSorted(j)
         outLivingSorted(j)=temp
      End If
    Next
  Next 
  'Temp array to store the new pinged and sorted by reponse time...
  Dim temparray
  ReDim temparray(UBound(outLivingSorted))
  For i = 0 To UBound(outLivingSorted) 
    temparray(i) = outLivingSorted(i)(0)
  Next
  outSortedByMs = temparray
End Function

示例输出 - 详细

[Performing Connectivity Test of Defined Servers]
Server: GOOGLE.COM [Passed]
Bytes Sent: 4
Bytes Recv: 3
Bytes Lost: 1
Min ms: 28
Max ms: 31
Average ms: 30
Server: YAHOO.COM [Passed]
Bytes Sent: 4
Bytes Recv: 4
Bytes Lost: 0
Min ms: 15
Max ms: 19
Average ms: 16
Server: CAT [Failed]
Server: LOCALHOST [Passed]
Bytes Sent: 4
Bytes Recv: 4
Bytes Lost: 0
Min ms: 0
Max ms: 0
Average ms: 0
In order fastest to slowest: 
LOCALHOST
YAHOO.COM
GOOGLE.COM

历史

  • 2007/12/20 - 现在使用二维数组,而不是具有两个公共成员的类。
  • 2008/02/05 - 修复了错误 - Windows 2000 ping 无法工作,无论如何返回码都是 0。
© . All rights reserved.