Jump to content

[Help] Making an auto ping tool


twig123

Recommended Posts

Hey guys,

I an in charge of managing several different system, that the user's are not always at. I have a project where I need to re-mediate issues with the systems by transferring a non-corrupted version of a file to the system that are online.

So, I an in need of a tool to be able to ping a list of Computer/Host names and export the pingable and unpingable lists, where I can then C$ into the system and replace the needed file.

Any ideas? does anything like this exist, or could someone help me automate this process?

Thanks in advance

Edited by twig123
Link to comment
Share on other sites


Ok... I was able to get the pinging part down with the .bat file below:

@echo off> pingLOG.txt

Echo Running Pings

for /f "tokens=*" %%I in (IPlist.txt) do call :pinger %%I
goto :eof

:pinger

echo %TIME% >> pingLOG.txt
ping -n 2 %1 >> pingLOG.txt

Can someone help me, I need to change this so that it filters the Computers/Hosts into either Online or Offline (separate files would be fine, or a way to sort them in 1 file would be great).

Thanks

Edited by twig123
Link to comment
Share on other sites

Thanks for moving my thread, I didnt know where it would go best.

***

I have 3 variables that can come of the ping tests:

"Ping request could not find host"

"Request timed out"

"Reply"

is there a way with 1 ping, that I can determine what the status is and write the 2 possible failures into 1 file, and any reply's into a dif. file?

Link to comment
Share on other sites

Ping! ... Pong?

Anyone out there in scripting land?

I have an idea of putting the ping output into a variable, and then righting some IF statements... but I dont know how to gather the ping results once the ping command has been give to be able to make it into a variable.

I would really appreciate any help that you could give me.

Thanks in advance

Edited by twig123
Link to comment
Share on other sites

Try this out, I wrote it for checking long lists of hosts (just put one host per line in a text file):

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)

This might not be at all what you wanted to do...but I'll offer anyway :) Just put one server/hostname per line in a file named servers.txt in the same folder as this script and you'll be good to go. It creates a .csv file you can open in excel with the results.

Link to comment
Share on other sites

This is what I have so far:

@echo off> OnlineLOG.txt
@echo off> OfflineLOG.txt

Echo ************************
Echo ** Running Ping Tests **
Echo ************************

for /f "tokens=*" %%I in (IPlist.txt) do call :pinger %%I
goto :eof

:pinger
ping -n 1 %1|find "Ping request could not find host"
if not errorlevel 1 goto :fail
ping -n 1 %1|find "Request timed out."
if not errorlevel 1 goto :fail
Echo %1>> OnlineLOG.txt

:fail
ping -n 1 %1|find "Reply"
Echo %1>> OfflineLOG.txt

..however it is buggy and slow becase it pings at least 2 times before confirming that the system is online. also the bug that I have is that ALL the hosts get written to the offlineLOG.txt instead of only the offline.

Any tips on making this better?

***

@kasandoro - dude... that is sweet. I guess VBs is better than batch scripting anyways.. :P

is there a way to auto sort the output within the script into online, no responce, or unknown host? that would be killer! ... and a status of current host would be cool as well if possible.

Edited by twig123
Link to comment
Share on other sites

Here is a working sample for your needs. You can change the way computers are read in to array if you want, now they are hard coded to the script.

'Define Objects
'---------------------------------------------------------------------------------------

Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")'
Set objNetwork = CreateObject("WScript.Network")

Const ForAppending = 8
'Create a txt file
'---------------------------------------------------------------------------------------

Set objFile = objFSO.OpenTextFile("C:\FalsePing.txt", ForAppending, True)

'List of computers

Dim IP_ARRAY()
'Change the size of the array also to here
'---------------------------------------------------------------------------------------
Redim IP_ARRAY(10)
i=0

'Array of target computers
'---------------------------------------------------------------------------------------
IP_ARRAY(0) = "10.13.56.14"
IP_ARRAY(1) = "10.13.56.2"
IP_ARRAY(2) = "10.13.56.3"
IP_ARRAY(3) = "10.13.56.4"
IP_ARRAY(4) = "10.13.56.5"
IP_ARRAY(5) = "10.13.56.6"
IP_ARRAY(6) = "10.13.56.7"
IP_ARRAY(7) = "10.13.56.8"
IP_ARRAY(8) = "10.13.56.9"
IP_ARRAY(9) = "10.13.56.10"
IP_ARRAY(10) = "10.13.59.11"



For Each Host in IP_ARRAY

If Ping(IP_ARRAY(i)) = False Then
'Write the offline computername to FalsePing.txt
'---------------------------------------------------------------------------------------
ObjFile.Writeline(IP_ARRAY(i) & " is not online")
i=i+1
Else
'Copy the needed file
'---------------------------------------------------------------------------------------
'Uncomment this line if you want to copy a file. Now it only writes that the computer is online
'objFSO.CopyFile "C:\SourceFile.txt" , "C$\", OverwriteExisting
ObjFile.Writeline(IP_ARRAY(i) & " is online")
i=i+1
End if
Next

Function objWMI(strComputer, strWQL)

Dim wmiNS, objWMIService

wmiNS = "\root\cimv2"

Set objWMIService = GetObject("winmgmts:\\" & strComputer & wmiNS) 'connect to WMI
Set objWMI = objWMIService.ExecQuery(strWQL) 'execute query
Set objWMIService = Nothing
End Function

Function Ping(strRmtPC)

Dim wmiQuery, objPing, objStatus, blnStatus

wmiQuery = "Select * From Win32_PingStatus Where Address = '" & strRmtPC & "'"

Set objPing = objWMI(".", wmiQuery)

For Each objStatus in objPing
If IsNull(objStatus.StatusCode) Or objStatus.Statuscode<>0 Then
blnStatus = False 'Not Reachable
Else
blnStatus = True 'Reachable
End If
Next

Ping = blnStatus
Set objPing = Nothing
End Function

Edited by geezery
Link to comment
Share on other sites

  • 1 month later...

Hi

I dunno if you have resolved this request but these are the changes I have made to your script.

It will sort your list into 3 files - Online, Offline and UnknownHost.

It works really well.

The script:

*****************************************************************

::Ping Test Multiple Computers

for /f "tokens=*" %%I in (IPList.txt) do call :pinger %%I

goto :eof

:pinger

ping -n 1 %1% |find "Reply from"

if not errorlevel 1 echo %1>> Online.txt

ping -n 1 %1% |find "Request timed out"

if not errorlevel 1 echo %1>> offline.txt

ping -n 1 %1% |find "could not find host"

if not errorlevel 1 echo %1>> Unknownhost.txt

::DONE

*****************************************************************

Regards

Link to comment
Share on other sites

  • 2 weeks later...
Hi

I dunno if you have resolved this request but these are the changes I have made to your script.

It will sort your list into 3 files - Online, Offline and UnknownHost.

It works really well.

The script:

*****************************************************************

::Ping Test Multiple Computers

for /f "tokens=*" %%I in (IPList.txt) do call :pinger %%I

goto :eof

:pinger

ping -n 1 %1% |find "Reply from"

if not errorlevel 1 echo %1>> Online.txt

ping -n 1 %1% |find "Request timed out"

if not errorlevel 1 echo %1>> offline.txt

ping -n 1 %1% |find "could not find host"

if not errorlevel 1 echo %1>> Unknownhost.txt

::DONE

*****************************************************************

Regards

This worked great for me. Thanks

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...