Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Set the TTL for a PING 1

Status
Not open for further replies.

JPJeffery

Technical User
May 26, 2006
600
GB
Hello again (it's been a while)

How can I specify a (larger than the default) Time To Live (TTL) in vbScript (for PINGing more distant servers, e.g. Beijing to London!)? It's easy to do from the command line but I've not been able to find out how to set the TTL in my vbScript.

The code I have so far (and yes, I realise there is some redundancy here) is as follows:
Code:
Sub vbPING(strHost)
    ' Check that all arguments required have been passed.

    if Ping(strHost) = True then
        Wscript.Echo "Host " & strHost & " contacted"
    Else
        Wscript.Echo "Host " & strHost & " could not be contacted"
    end if

End Sub
'---------------------------------------------------------
Function Ping(strHost)

    dim objPing, objRetStatus

    set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery _
      ("select * from Win32_PingStatus where address = '" & strHost & "'")

    for each objRetStatus in objPing
        if IsNull(objRetStatus.StatusCode) or objRetStatus.StatusCode<>0 then
            Ping = False
            'WScript.Echo "Status code is " & objRetStatus.StatusCode
        else
            Ping = True
            'Wscript.Echo "Bytes = " & vbTab & objRetStatus.BufferSize
            'Wscript.Echo "Time (ms) = " & vbTab & objRetStatus.ResponseTime
            'Wscript.Echo "TTL (s) = " & vbTab & objRetStatus.ResponseTimeToLive
        end if
    next
End Function

JJ
[small][purple]Variables won't. Constants aren't[/purple]
There is no apostrophe in the plural of PC (or PST, or CPU, or HDD, or FDD, and so on)[/small]
 
Something like this ?
("select * from Win32_PingStatus where address = '" & strHost & "' and TimeToLive=250")

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Like PHV said....side note: Win32_PingStatus is weird because you set the parameters for the ping with the Where and build on it like a query using AND

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Thanks, chaps. I've also added in the ResponseTimeToLive option (on the grounds that it seemed like a good idea to wait for an appropriate length of time for the reply)
Code:
("select * from Win32_PingStatus where address = '" & strHost & "' and TimeToLive=250 and ResponseTimeToLive=500")
Having added these in the PINGs are failing on even local servers! Do you happen to know what the default values are?

JJ
[small][purple]Variables won't. Constants aren't[/purple]
There is no apostrophe in the plural of PC (or PST, or CPU, or HDD, or FDD, and so on)[/small]
 
ResponseTimeToLive is not a key, so don't use it in the WHERE clause.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top