如何ping多个服务器并返回其IP

时间:2009-08-26 06:48:26

标签: vbscript

我有一个txt文件,其中包含不同服务器名称的列表(每行一个服务器名称)。

我很想知道是否可以编写脚本来ping所有服务器并将IP地址输出到文本文件。

任何提示都将不胜感激。

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

我终于通过here找到了一个VB脚本。

基本上,脚本将从servers.txt中检索服务器列表(此文件逐行包含主机名)并生成一个CSV文件,其中包含所有服务器的综合ping结果。

注意:不要将脚本命名为'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)