返回列表 上一主題 發帖

測試IP

回復 1# linderlong
試試看用下列 Function 看看:
  1. Public Sub iptest()
  2.     MsgBox Ping("10.18.22.5")
  3. End Sub
複製代碼
  1. Private Function Ping(strAddr As String) As String
  2.     Dim strTmpFile As String
  3.    
  4.     strTmpFile = Environ("TEMP") & "\PingResult.txt"   '  準備建暫存檔在 Windows Temp 目錄下
  5.     '  建立 WScript 物件 Shell 類別 , 使用 Run 方法 , 呼叫外部指令 Ping ( 並等待其執行結束 )
  6.     CreateObject("WScript.Shell").Run Environ("COMSPEC") & " /c PING " & strAddr & " > " & strTmpFile, 0, True
  7.     '  PS : "Ping IP位置 > XX檔案" 這裡是使用 > 來將 Ping 結果寫到檔案裡
  8.     '  ( 當然也可 Call API CreatePipe 來讀取命令結果 )
  9.     '  Ping 是一種公用程式,可以驗證一或多個遠端主機連線。
  10.     '  Ping 指令用 ICMP 回應要求和回應回覆套裝軟體來決定網路上的特殊 IP 系統是否正常運作。
  11.     '  Ping 對診斷 IP 網路或路由器失敗非常有用。
  12.     '  Internet Control Message Protocol (ICMP)
  13.     '  TCP/IP 組件中必要的一種維護通訊協定,可報告錯誤並允許簡易連線。
  14.     '  Ping 工具用來執行 TCP/IP 疑難排解的 ICMP。
  15.     '  詳細說明及用法可參考:
  16.     '  ms-its:%WINDIR%\Help\ntcmds.chm::/ping.htm
  17.     '  ms-its:%WINDIR%\Help\ntcmds.chm::/ping.htm
  18.     '  ms-its:%WINDIR%\Help\tcpip.chm::/sag_TCPIP_pro_PingConnect.htm
  19.     '  ms-its:%WINDIR%\Help\tcpip.chm::/sag_TCPIP_pro_Ping.htm
  20.     '  建立檔案系統物件,用來開啟暫存文字檔,並讀取內容 ( 取回 Ping的結果 )
  21.     Ping = CreateObject("Scripting.FileSystemObject").OpenTextFile(strTmpFile).ReadAll
  22.     Ping = Replace(Ping, vbCrLf, "", 1)
  23.    
  24.     On Error Resume Next
  25.     Kill strTmpFile         '  刪除暫存檔
  26. End Function
複製代碼

TOP

回復 3# linderlong
我連上 Google 約需五秒,如下圖所示:

如以 .1 到 .10 要 45 秒 (偵測外部 IP),
應是合理範圍。

TOP

本帖最後由 c_c_lai 於 2013-8-28 14:33 編輯

回復 5# linderlong
你試著用 Shell command 的方式執行看看:
  1. Sub Test()
  2.     Dim strIP As String, sp As Variant, RetVal
  3.     Dim cts As Integer, xi As Integer
  4.    
  5.     strIP = "100.18.22.1,100.18.22.2,100.18.22.3,100.18.22.4,100.18.22.5,100.18.22.6,100.18.22.7,100.18.22.8,100.18.22.9,100.18.22.10"
  6.    
  7.     sp = Split(strIP, ",")
  8.     cts = 0
  9.     For xi = 0 To UBound(sp)
  10.         cts = cts + 1
  11.         '  RetVal = Shell("command.com /c Ping -n 1 100.18.22.1 > D:\iptest\1.txt")
  12.         RetVal = Shell("command.com /c Ping -n 1 " & sp(xi) & " > " & ThisWorkbook.Path & "\iptest" & cts & ".txt")
  13.         工作表1.Cells(cts, 1) = sp(xi)
  14.     Next xi
  15. End Sub
複製代碼

TOP

回復 8# linderlong
請將 Cells(i, 1) 明確指定 工作表單之歸屬。
譬如: Sheet1.Cells(i, 1) 等。

TOP

回復 10# linderlong
  1.         RetVal = Shell("command.com /c Ping -n 1 " & 工作表1.Cells(cts, 1) & " > " & ThisWorkbook.Path & "\iptest" & cts & ".txt")
複製代碼
經實地在 Excel 2010  下測試是無誤的,
請檢查你的執行錯誤點是在哪一段?

TOP

回復 10# linderlong

TOP

回復 14# linderlong
試著在 WinX 64 位元環境下測試是否OK?

TOP

回復 14# linderlong
你直接將 Debug 畫面完整 Copy 上傳 (需有錯誤點指向)

TOP

回復 18# linderlong
RetVal  上行插入 DoEvents 試試看。
  1.     DoEvents
  2.     RetVal = Shell("command.com /c Ping -n 1 " & Cells(i, 1) & " > " & ThisWorkbook.Path & "\" & i & ".txt")
複製代碼

TOP

回復 20# linderlong
這是你網路權限不夠的緣故。
亦即 Users' 的權限不足無法指到 Windows/system32/ 的路徑。

TOP

        靜思自在 : 有心就有福,有願就有力,自造福田,自得福緣。
返回列表 上一主題