Login | |
|
 |
parse ping output - 11/22/2005 7:58:54 AM
|
|
 |
|
| |
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
|
|
| |
|
|
|
 |
RE: parse ping output - 11/23/2005 1:10:36 AM
|
|
 |
|
| |
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.
|
|
| |
|
|
|
 |
RE: parse ping output - 11/23/2005 1:59:53 AM
|
|
 |
|
| |
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
|
|
| |
|
|
|
| |
|
|
 |
|
 |
|
|