|
The following VBScript code implements and tests a function for determining (based on the results of a "ping" command) if a remote host is reachable. As coded below, the example tests to see if this web site is online. By modifying the "theHost" variable, you could check the status of any other site you care about. The script accomplishes what it does by having Windows call the command-line "ping" utility, piping the results to a text file in a temporary folder. It then scans the text file to see if a reply was received to the ping command by the remote host. If so, the function returns a True value. If not, it returns False. option explicit dim theHost theHost = "www.mikesalsbury.com" if reachable(theHost) then wscript.echo theHost & " is reachable" else wscript.echo theHost & " is not reachable" end if function reachable(HostName)
dim wshShell, fso, tfolder, tname, TempFile, results, retString, ts ' Set values for the following constants used in the script. Const ForReading = 1, TemporaryFolder = 2 ' Unless we change the value, assume the host is unreachable. reachable = false ' Create a Shell object. set wshShell=wscript.createobject("wscript.shell") ' Create a Filesystem object set fso = CreateObject("Scripting.FileSystemObject") ' Get a temporary folder and file Set tfolder = fso.GetSpecialFolder(TemporaryFolder) tname = fso.GetTempName TempFile = tfolder & tname ' Use the Shell to ping the host store the result in the temporary file wshShell.run "cmd /c ping -n 3 -w 1000 " & HostName & ">" & TempFile,0,true set results = fso.GetFile(TempFile) set ts = results.OpenAsTextStream(ForReading) ' Scan the results of the ping command to find a "Reply" message, which ' indicates that the ping was successful. do while ts.AtEndOfStream <> True retString = ts.ReadLine if instr(retString, "Reply")>0 then reachable = true exit do end if loop ts.Close ' Get rid of the temporary file. results.delete end function
Related Blogs:
Related Links:
|