使用WSH呼叫系統的Ping指令,將Ping的結果重新導向到一個文字檔中去,再把文字檔顯示到網頁中,具體做法如下:
首先, 建一個.BAT檔案(例如:myPing.BAT:),這個檔案要在ASP中呼叫,檔案程式碼如下:
ping -a %1 > d:/INetPub/cgi-bin/%2.txt
(%1)是將來要ping的位址, (%2)是儲存ping結果的檔案. 以下是ASP的程式碼:
< %
Set FileSys = Server.CreateObject(Scripting.FileSystemObject)
FileName = FileSys.GetTempName
Set WShShell = Server.CreateObject(WScript.Shell)
IP = xxx.xxx.xxx.xxx '你要ping的位址
RetCode = WShShell.Run(d:/Inetpub/cgi-bin/myPing.bat & IP & & FileName, 1, True)
if RetCode = 0 Then
'沒有錯誤
else
Response.Redirect PingErrors.htm
end if
Set TextFile = FileSys.OpenTextFile(d:/InetPub/cgi-bin/ & FileName & .txt, 1)
TextBuffer = TextFile.ReadAll
For i = 1 to Len(TextBuffer)
If Mid(TextBuffer,i,1) = chr(13) Then
Response.Write(
)
else
Response.Write(Mid(TextBuffer,i,1))
end if
Next
TextFile.Close
FileSys.DeleteFile d:/Inetpub/cgi-bin/ & FileName & .txt
% >