tags:

views:

1016

answers:

2

Hi,

I have a txt file which contains a list of different server names (one server name per line).

I am wonrdering if I can write a script to ping all the servers and output the IP addresses to a text file.

Any hints would be greatly appreciated.

A: 

Hope this can help http://smartproteam.com/shell-script-ping-multiple-hosts/

KS
A: 

I finally found a VB script via here.

Basically, the script will retrieve a list of servers from servers.txt (this file contains the host names line by line) and generate a CSV file which contains comprehensive ping results of all servers.

NOTE: Do NOT name the script as 'ping.vbs' as it will use 'ping.vbs' to ping the hosts instead of 'ping' command.

Here is the script:

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)
jiaoziren
Essentially this script captures and parses the screen output of command-line command ping.
Peter Mortensen