Help - Search - Members - Calendar
Full Version: VBScript - Ping multiple ip addresses and machine names
MSFN Forums > Coding, Scripting and Servers > Programming (C++, Delphi, VB, etc.)

   
Google Internet Forums Unattended CD/DVD Guide
tlancaster
Hello

I work for a company that has staff in India who print to networked printers in UK.

The way it worked is they print to a local workstation which is configured to route to an ip address of a printer in the UK.

There is presently 13 printers set up to receive prints.

What I'd like to do is set up a VB Script to ping the workstations and our printers to see if they are online. Then write the results to an excel spreadsheet.

I'd like to spread sheet to have 3 collumns:

Alias (i.e. INDIA 1), IP Address/Hostname (V011-V02010V.banking.uk), Result of ping (reply received/not received)

Would someone have a script they've designed to implement the above?

Many thanks,

Tom
kasandoro
It's funny you'd ask for this, as I posted it today in another thread smile.gif This does exactly what you asked, but I don't think it provides the results you asked for specifically. Also, it creates a .csv file, but that can easily be opened in excel for easy viewing. You can obviously edit it for your needs.

Anyway, here you go:

CODE
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 pinged per 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:\Program Files\Microsoft Office\OFFICE11\excel.exe """&logfile)


Create a file named servers.txt in the same folder as this script, with one computername/ip address that you'd like to ping per line. That should be all you need smile.gif
timmio
hello, im a complete noob but i was googling for this exact thing and was very happy when i saw your code, the problem is when i try to run it my computer gives me this error

script: C:\ping\ping.vbs
line: 13
char: 1
error: Permission denied
Code: 800A0046
Source: Microsoft VBScript runtime Error

I was running in adminstrator.

i know this is thread necromancy but i couldnt find anywere else to ask about it.
Yzöwl
The script is working fine here for me, I'd guess that you have a copy/paste error.

You could try downloading this prettied version of it if you're still having difficulty!
crahak
QUOTE (Yzöwl @ Jul 21 2008, 10:46 AM) *
I'd guess that you have a copy/paste error.

Seeing the actual error message, probably not. He gets a "Permission denied" error on a line that uses the CreateTextFile method of FSO. Odds are either:
-there's already a file with such a name, and it has the read-only attribute set (happens a lot when you copy stuff from a CD...)
or
-he doesn't have sufficient permissions to create or overwrite that file (ACLs)

But anyways, personally, I either use the Win32_PingStatus WMI class because you can get more detailed infos, more detailed error codes, and that there's no text parsing involved (doesn't work with pre-XP OS'es admittedly) , or even just plain old nmap e.g. "nmap -iL servers.txt -SP"
Google Internet Forums Unattended CD/DVD Guide
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.