Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Redirect a Ping command to a log or text file?

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,3292
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Redirect a Ping command to a log or text file?
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 Redirect a Ping command to a log or text file? - 6/8/2005 6:57:37 AM   
  aiu0835

 

Posts: 4
Score: 0
Joined: 6/8/2005
From: USA
Status: offline
Hello,

I'm quite new with WSH or VBscript. I'm trying to write a short WSH program to read a list of server name in a text file and issue the Ping command against that. I'd like to redirect it to a log or text file, so I have a record of which server does not respond to the Ping. However, I could not get that to work. Can anyone help me with that? Here is my WSH program:

strInputFile = "C:\AIU0835\Downloads\Scripting\quixtarservers.txt"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextStream = objFSO.OpenTextFile(strInputFile, 1)
Set objShell = CreateObject("Wscript.Shell")

arrServer = Split(objTextStream.ReadAll, vbCrLf)
objTextStream.Close

For Each strServer in arrServer
intCMD = objShell.Run("Ping " & strServer)
WScript.Sleep 100
Next

Set objFSO = Nothing
Set objTextStream = Nothing
Set objShell = Nothing
intCMD = 0
WScript.Echo "Done"

Thanks,
PT
 
 
Post #: 1
 
 Re: Redirect a Ping command to a log or text file? - 6/8/2005 7:42:59 AM   
  mbouchard


Posts: 1922
Score: 16
Joined: 5/15/2003
From: USA
Status: offline
The easist way to do that would be to use cmd prompt redirector.

intCMD = objShell.Run("cmd /c ping " & strServer & " >c:\" & strServer & "_PING.txt",1,TRUE)

Do a search and look for a ping post that I did, it is more involved but may have what you are looking for.

(in reply to aiu0835)
 
 
Post #: 2
 
 Re: Redirect a Ping command to a log or text file? - 6/8/2005 12:10:05 PM   
  netmarcos

 

Posts: 55
Score: 0
Joined: 12/7/2004
From: USA
Status: offline
This an old scriipt that should give you some good ideas. An input file with the names or IP addresses in a single column named compname.txt is required.
      

(in reply to aiu0835)
 
 
Post #: 3
 
 Re: Redirect a Ping command to a log or text file? - 6/13/2005 3:02:59 AM   
  Country73


Posts: 735
Score: 10
Status: offline
This is what I use for any script that I generally have to run against multiple machines. (Basically my "Script Template")
This will also create a countdown for machines remaining. Very handy when needing to run a lengthy script against 2000+ machines.

Set oFS = CreateObject("Scripting.FileSystemObject")
Set wshShell = WScript.CreateObject("WScript.Shell")
Const strScript = "Script Template"
Const PCL = "pclist.txt"
Const OFF = "offline.txt"

'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
' Force script to run in "CScript" mode
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
If Instr(1, WScript.FullName, "CScript", vbTextCompare) = 0 Then
Set shell = CreateObject("WScript.Shell")
shell.Run "cscript """ & WScript.ScriptFullName & """", 1, False
WScript.Quit
End If
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

' User input to determine if script is ran against single or multiple machines.
strComputer = InputBox("Which machine do you wish to run this script against?" & vbcrlf _
& vbcrlf & "Leave blank to run against " & chr(34) & "pclist.txt" & chr(34),strScript,"")

If strComputer = "" Then
If oFS.FileExists(PCL)Then
'-Gets a count for machines to run against-
Set file = oFS.GetFile(PCL)
Set pc = file.OpenAsTextStream(1,TristateUseDefault)
Do While pc.AtEndOfStream <> True
strComputer = Trim(pc.ReadLine)
strCount = strCount + 1
Loop
'-Pull netbios to run script against-
Set pc = file.OpenAsTextStream(1,TristateUseDefault)
Do While pc.AtEndOfStream <> True
strComputer = Trim(pc.ReadLine)
strCount = strCount - 1
wscript.echo strCount & " " & strComputer
Call PingMe(strComputer,strCount)
Loop
Else
wscript.echo "No " & PCL & " found!" & vbcr & "Script will now close."
wscript.quit
End If
Else
strCount = ""
Call PingMe(strComputer,strCount)
End If
wscript.echo "Finished"

'Functions
'------------------------------------------------------------------------------
Function PingMe(strComputer,strCount)'-Check to make sure machine is online first
Set cPingResults = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2").ExecQuery("SELECT * FROM Win32_PingStatus WHERE Address = '" + strComputer + "'")
For Each oPingResult In cPingResults
If oPingResult.StatusCode = 0 Then
wscript.echo "call function required"
Else
call LogEvent(strComputer,"Offline")
End If
Next
End Function
'+++++++++++++++++++OFFLINE+++++++++++++++++++++++
Function LogEvent(pc,msg)'If OFFLINE, create a log
Set strLogFile = oFS.OpenTextFile(OFF,8,True)
strLogFile.WriteLine pc & vbTab & msg
strLogFile.Close
End Function
'+++++++++++++++++++++++++++++++++++++++++++++++++

(in reply to aiu0835)
 
 
Post #: 4
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Redirect a Ping command to a log or text file? Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts