Jan 23, 2008

網路連線測式(Ping by List)

PingList.vbs
Msg = "站點" & vbTab & paddingStr("位址",27) & vbTab & paddingStr("IP",15) & vbTab & "回應時間" & vbNewLine
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set objXML = CreateObject("Microsoft.XMLDOM")
objXML.async = False
objXML.Load "ipList.xml"
If objXML.parseError.errorCode <> 0 Then
 WScript.Echo("設定檔載入錯誤")
End If

Set objList = objXML.getElementsByTagName("*")
Set nodes = objList.item(i).selectSingleNode("//site").childNodes

For Each node In nodes
 Dim ProtocolAddress
 Dim ResponseTime
 Set objNamedNodeMap = node.attributes  
 Set objPing = objWMIService.ExecQuery("select * from Win32_PingStatus where address = '" & node.text & "'")
 For Each objStatus in objPing
  ProtocolAddress = objStatus.ProtocolAddress
  ResponseTime = objStatus.ResponseTime
 Next 
 Msg = Msg & objNamedNodeMap.item(0).text & vbTab & paddingStr(node.text,25) & vbTab & paddingStr(ProtocolAddress,15) & vbTab & ResponseTime & vbNewLine 
Next
WScript.Echo(Msg)

Function paddingStr(str,lenght)
 If Len(str) > lenght then
  paddingStr = str
 Else
  'todo:double byte analysis
  paddingStr = str &  Space(lenght - Len(str))
 End If
End Function


ipList.xml
<?xml version="1.0" encoding="big5"?>
<site>
<item name="HiNet">www.hinet.net</item>
<item name="Yahoo">tw.yahoo.com</item>
<item name="Pchome">www.pchome.com</item>
</site>

No comments: