Gå til innhold

Anbefalte innlegg

Jeg er på jakt etter et script som kan pinge en adresse og logge dette in en tekstfil. Søkte litt og fant to script som jeg limt litt sammen.

 

Det jeg plages med er at resultatet lagres i en tabel, men når dette skal skrives til tekstfilen er det kun de to første linjene som blir logget.

 

Dim strHost, strOutputPath, strOutputFileName, strPingOutput, arrPingOutput, intLatency, WSHShell, Message, Title 

Message = "Skriv inn adresse til siden, f.eks www.vg.no" 
Title = "Ping tester"
Text1 = "Sorry, user input was canceled"
Text2 = "I got the input:" + vbCRLF

Set WSHShell = WScript.CreateObject("WScript.Shell")

result = InputBox(Message,Title,"www.vg.no", 100, 100)

strHost= result
strOutputPath="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 10 " & 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")+1)) 
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

Lenke til kommentar
Videoannonse
Annonse

Skal bruke dette ute hos en kunde som har under gjennomsnittlig kunnskap på alt annet enn fag applikasjoner. Derfor skal han starter scriptet, det skal komme opp en rute hvor han skriver inn en adresse, scriptet pinger adresse og skriver dette til en log.

 

Fant uansett ut av det :)

Endret av phoen|x
Lenke til kommentar

Skal du lagre hele pingresultatet eller bare deler av det? Iallfall, her har du et eksempel på at du får lagret og sett hele resultatet av ping www.vg.no.

 

result = pinghost("www.vg.no")
save(result)
show

Function PingHost(input) 
Dim strCommand, objShell, objExec 
strCommand = "%comspec% /c ping -n 10 " & input 
Set objShell = CreateObject("WScript.Shell") 
Set objExec = objShell.Exec(strCommand) 
PingHost = objExec.StdOut.ReadAll 
End Function 

sub save(input)
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile("c:\pingResult.txt", 2, True)
f.Write input
f.close
end sub

sub show
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run ("notepad.exe c:\pingResult.txt")
end sub

Lenke til kommentar

Opprett en konto eller logg inn for å kommentere

Du må være et medlem for å kunne skrive en kommentar

Opprett konto

Det er enkelt å melde seg inn for å starte en ny konto!

Start en konto

Logg inn

Har du allerede en konto? Logg inn her.

Logg inn nå
×
×
  • Opprett ny...