2009-08-26 76 views
0

我有一個txt文件,其中包含不同服務器名稱的列表(每行一個服務器名稱)。如何ping多個服務器並返回它們的IP

我很想知道是否可以編寫腳本來ping所有服務器並將IP地址輸出到文本文件。

任何提示將不勝感激。

+3

雖然你說的「平」,你的實際需要來ping或只是解析IP地址? – 2009-08-26 06:53:30

回答

0

我終於通過here發現了一個VB腳本。

基本上,腳本將從servers.txt中檢索服務器列表(該文件逐行包含主機名稱)並生成一個包含所有服務器的綜合ping結果的CSV文件。

注意:不要將腳本命名爲'ping.vbs',因爲它將使用'ping.vbs'來ping主機而不是'ping'命令。

下面是腳本:

Const ForReading = 1 
Set objFSO = CreateObject("Scripting.FileSystemObject") 
Set objShell = CreateObject("WScript.Shell") 
If not objFSO.FileExists("servers.txt") THEN 
wscript.echo "Please create a file named 'servers.txt' with one PC name to be pingedper line,"&_ 
vbcrlf&"with a hard return at the end of each line." 
wscript.quit 
end if 
tempobj="temp.txt" 


Set objTextFile = objFSO.OpenTextFile("servers.txt", ForReading) 
logfile="results.csv" 
Set ofile=objFSO.CreateTextFile(logfile,True) 
strText = objTextFile.ReadAll 
objTextFile.Close 
wscript.echo "Ping batch starting, please be patient. This could take some time to"&_ 
vbcrlf&"finish, depending on the number of hosts to check. You "_ 
&"will be "&vbcrlf&"notified upon the completion of this script." 
ofile.WriteLine ","&"Ping Report -- Date: " & Now() & vbCrLf 
arrComputers = Split(strText, vbCrLF) 
for each item in arrcomputers 
objShell.Run "cmd /c ping -n 1 -w 1000 " & item & " >temp.txt", 0, True 
Set tempfile = objFSO.OpenTextFile(tempobj,ForReading) 
Do Until tempfile.AtEndOfStream 
temp=tempfile.readall 
    striploc = InStr(temp,"[") 
     If striploc=0 Then 
      strip="" 
     Else 
      strip=Mid(temp,striploc,16) 
      strip=Replace(strip,"[","") 
      strip=Replace(strip,"]","") 
      strip=Replace(strip,"w"," ") 
      strip=Replace(strip," ","") 
     End If  

     If InStr(temp, "Reply from") Then 
      ofile.writeline item & ","&strip&","&"Online." 
     ElseIf InStr(temp, "Request timed out.") Then 
      ofile.writeline item &","&strip&","&"No response (Offline)." 
     ELSEIf InStr(temp, "try again") Then 
      ofile.writeline item & ","&strip&","&"Unknown host (no DNS entry)." 

     End If 
     Loop 
     Next 
tempfile.close 
objfso.deletefile(tempobj) 
ofile.writeline 
ofile.writeline ","&"Ping batch complete "&now() 
wscript.echo "Ping batch completed. The results will now be displayed." 
objShell.Run("""C:\YOUR OFFICE INSTALL PATH\Microsoft Office\Office10\excel.exe """&logfile) 
+0

本質上,此腳本捕獲並分析命令行命令ping的屏幕輸出。 – 2010-02-21 12:55:29

相關問題