Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


parse ping output

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

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> parse ping output
  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 >>
 parse ping output - 11/22/2005 7:58:54 AM  1 votes
  chris10

 

Posts: 3
Score: 2
Joined: 11/22/2005
Status: offline
Hi,

I am looking to run a background service to ping a network device, parse the output and pass the latency to a text file.  The purpose of this is that MRTG can then plot the network latency.  http://people.ee.ethz.ch/~oetiker/webtools/mrtg/mrtg-reference.html#item_external_monitoring_scripts.  The script needs to update the file or re-run every 5 minutes.

In the past, I have achieved the same using a series of batch files -  is both cumbersome and complex involving unnecessary I/O and file writing.  I am looking to use a WSH Script to run in the background and perform the task - hopefully using less resources.

I have almost no experience of WSH or VBS - So far I have written (& robbed) a very rough WSH script which achieves the basics, but I have 2 questions...

1)  What's the best way of running a WSH as a background task?  ie no DOS box - preferably using instsvr to install & start/stop

2)  What's the most efficient way of running the process every 5 mins?

I would really welcome any help advice in achieving the above.

Many thanks in advance

Chris10






''file <PingLog.vbs>
Dim strHost, strOutputPath, strOutputFileName, strPingOutput, arrPingOutput, intLatency

strHost="www.google.com"
strOutputPatch="c:\temp"
strOutputFileName="outputfilename.txt"
strPingOutput = PingHost(strHost)
arrPingOutput = StringToArray(strPingOutput)
intLatency =  GetLatency(arrPingOutput(3))
Call WriteMRTGFile(intLatency,intLatency,strHost,"Unknown", strOutputPath, strOutputFileName)

Function PingHost(input)
''starts a dos box and pings the host, returning the full output of the ping command
Dim strCommand, objShell, objExec
strCommand = "%comspec% /c ping -n 1 " & input
Set objShell = CreateObject("WScript.Shell")
Set objExec = objShell.Exec(strCommand)
PingHost = objExec.StdOut.ReadAll
End Function

Function StringToArray(input)
''converts the multiline ping output to an array so that it can be parsed line by line
StringToArray = Split(input,vbCrLf)
End Function

Function GetLatency(input)
''cuts the preceeding & succeeding text, leaving just the latency (integer) remaining
Dim strLatencyTrimedLeft
   strLatencyTrimedLeft = mid(input,(InStr(input,"time")+5))
GetLatency = mid(strLatencyTrimedLeft,1,(InStr(strLatencyTrimedLeft,"ms")-1))
End Function

Sub WriteMRTGFile(var1,var2,HostName,SysUpTime,path,file)
''write results to file in MRTG format
Dim oFSO, oFile
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFile = oFSO.CreateTextFile(oFSO.BuildPath(path,file))
oFile.Write var1 & vbCrLf
oFile.Write var2 & vbCrLf
oFile.Write HostName & vbCrLf
oFile.Write SysUpTime & vbCrLf
oFile.Close : Set oFile = Nothing
Set oFSO = Nothing
Set oFile = Nothing
End Sub

''EoF












 
 
Post #: 1
 
 RE: parse ping output - 11/22/2005 9:13:09 PM   
  crazymatt

 

Posts: 287
Score: 0
Joined: 3/4/2005
From:
Status: offline
Why do you want it as an service? Are u gonna run it on computers other than yours?

1) Your first question is discussed here: http://www.visualbasicscript.com/m_2877/mpage_1/key_/tm.htm#2877
Read that thread for more info.

2) Either you could run the script with an AT command i guess or just start it as an normal script (without making it a service) and put a loop and a wait (for 5 min or what you want) in it and let it just run.

/Matt

_____________________________

-There is only 10 sorts of people, those who understand binary and those who dont.

(in reply to chris10)
 
 
Post #: 2
 
 RE: parse ping output - 11/23/2005 1:10:36 AM  1 votes
  mbouchard


Posts: 1804
Score: 12
Joined: 5/15/2003
From: USA
Status: online
If you don't want a dos box you can't use Exec as there is no way to hide the dos box.  You can use run and hide the dos box but would need to redirect your ping to a text file, open said text file and use readall/readline to go through it.  Do a quick search on Ping and you should find a post that I made on doing this.

_____________________________

Mike

For useful Scripting links see the Read Me First stickey!

Always remember Search is your friend.

(in reply to crazymatt)
 
 
Post #: 3
 
 RE: parse ping output - 11/23/2005 1:59:53 AM  1 votes
  ginolard


Posts: 1005
Score: 21
Joined: 8/10/2005
Status: offline
You can also use WMI to "ping" a machine and get the result that way.

Here is a function that checks if a machine is online and returns True or False.

Dim strComputer
 strComputer="foo"

Function IsConnectible

'Check if the remote machine is online.
    Dim objPing,objStatus
   
    Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}")._
        ExecQuery("select Replysize from Win32_PingStatus where address = '" & strComputer & "'")

    For Each objStatus in objPing
        If  IsNull(objStatus.ReplySize) Then
            IsConnectible=False
         Else
            IsConnectible = True
          End If
    Next
    Set objPing=Nothing
    Set objStatus=Nothing
End Function

(in reply to mbouchard)
 
 
Post #: 4
 
 RE: parse ping output - 12/7/2005 12:43:33 AM   
  chris10

 

Posts: 3
Score: 2
Joined: 11/22/2005
Status: offline
Hey thanks all for your answers.  Sorry so late in passing on my thanks.  Scripts fell to the bottom of the pile.

Regarding running a wscript as a service, the svrany/instsvr thread really helped.  I have used svrany in the past with batch files but I'm new to this VBS/Wscript stuff.  Thanks CrazyMatt.

Mike,  I was trying to cut down on amount of disk I/O - my batch files were constantly writing a temp file here or there to disk hence looking at WScript.  I'll weighup a DOS box vs one more temp file and continue from there.

Gino, Cheers for the WMI script.  Neat.  I was really looking for latency, but I am sure I can find a use for that sometime.  Like autosending an email on device down.  handy.

Cheers Guys

Chris10

< Message edited by chris10 -- 12/30/2005 3:16:48 AM >

(in reply to chris10)
 
 
Post #: 5
 
 RE: parse ping output - 12/23/2005 12:42:02 AM   
  chris10

 

Posts: 3
Score: 2
Joined: 11/22/2005
Status: offline
All worked nicely and I am now polling the latency to servers via Ping, WGET (for HTTP Latency) and BIND DIG (for DNS Latency), outputting as a file and graphing with MRTG.



>>What's the best way of running a WSH as a background task?  ie no DOS box
http://www.firedaemon.com/
Use FireDaemon to run WSCRIPT or CSCRIPT and pass the VBS file as a parameter.  The VBScript sits happily in the background, does not disturb the console and autorestarts if the service ever closes.  Works much better than svrany/instsvr.



>>What's the best way of scripting Ping?
If you want to do any scripting with Ping under XP, use the WMI control rather than the command line tool.  It works very well indeed.  You can pretty much read anything you could with the command line.  Cheers Gino!
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/ping_provider.asp
http://www.microsoft.com/technet/scriptcenter/resources/qanda/sept04/hey0914.mspx


>>What's the best way of scripting & parsing other command line tools such as WGET, NSLookup & Dig.exe?

Use the EXECmethod and parse the STDOut & STDErr into an array.  Don't use the Run method.  Don't try and redirect the output of the command to a text file (nslookup www.google.com > myoutput.txt) and parsing the file.  It's hard work and does not work well when you turn your script into a service.  The EXEC method with STDOut/STDErr works much, much better.  This does work for Ping as well under W2K.


Thankyou to you all for your input,

Chris

< Message edited by chris10 -- 12/23/2005 12:46:04 AM >

(in reply to chris10)
 
 
Post #: 6
 
 
 
  

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 >> parse ping output 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