Jump to content

Advanced Ping function


Recommended Posts

Hello,

i'he make a script with check the connectivity from pc to another pc on lan or wan.

The function used is "ping" on autoit.

The current ping function allow only ip address and time out;

I need also those parameters:

- Buffer data size by send to host, and other

I Could' not use the ping os MS Windows, because don't report the date , and hour a ns seconds by ping event,

on my script it's essentials!!

Can help me?

Thank!

Link to comment
Share on other sites

This is something I wrote in 2008 (just made some improvements now).

#include <WinAPI.au3>

Global Const $IP_SUCCESS = 0
Global Const $IP_STATUS_BASE = 11000
Global Const $IP_BUF_TOO_SMALL = ($IP_STATUS_BASE + 1)
Global Const $IP_DEST_NET_UNREACHABLE = ($IP_STATUS_BASE + 2)
Global Const $IP_DEST_HOST_UNREACHABLE = ($IP_STATUS_BASE + 3)
Global Const $IP_DEST_PROT_UNREACHABLE = ($IP_STATUS_BASE + 4)
Global Const $IP_DEST_PORT_UNREACHABLE = ($IP_STATUS_BASE + 5)
Global Const $IP_NO_RESOURCES = ($IP_STATUS_BASE + 6)
Global Const $IP_BAD_OPTION = ($IP_STATUS_BASE + 7)
Global Const $IP_HW_ERROR = ($IP_STATUS_BASE + 8)
Global Const $IP_PACKET_TOO_BIG = ($IP_STATUS_BASE + 9)
Global Const $IP_REQ_TIMED_OUT = ($IP_STATUS_BASE + 10)
Global Const $IP_BAD_REQ = ($IP_STATUS_BASE + 11)
Global Const $IP_BAD_ROUTE = ($IP_STATUS_BASE + 12)
Global Const $IP_TTL_EXPIRED_TRANSIT = ($IP_STATUS_BASE + 13)
Global Const $IP_TTL_EXPIRED_REASSEM = ($IP_STATUS_BASE + 14)
Global Const $IP_PARAM_PROBLEM = ($IP_STATUS_BASE + 15)
Global Const $IP_SOURCE_QUENCH = ($IP_STATUS_BASE + 16)
Global Const $IP_OPTION_TOO_BIG = ($IP_STATUS_BASE + 17)
Global Const $IP_BAD_DESTINATION = ($IP_STATUS_BASE + 18)
Global Const $IP_ADDR_DELETED = ($IP_STATUS_BASE + 19)
Global Const $IP_SPEC_MTU_CHANGE = ($IP_STATUS_BASE + 20)
Global Const $IP_MTU_CHANGE = ($IP_STATUS_BASE + 21)
Global Const $IP_UNLOAD = ($IP_STATUS_BASE + 22)
Global Const $IP_ADDR_ADDED = ($IP_STATUS_BASE + 23)
Global Const $IP_GENERAL_FAILURE = ($IP_STATUS_BASE + 50)
Global Const $MAX_IP_STATUS = ($IP_STATUS_BASE + 50)
Global Const $IP_PENDING = ($IP_STATUS_BASE + 255)
Global Const $PING_TIMEOUT = 500
Global Const $WS_VERSION_REQD = 0x101
Global Const $MIN_SOCKETS_REQD = 1
Global Const $SOCKET_ERROR = -1
Global Const $INADDR_NONE = 0xFFFFFFFF
Global Const $MAX_WSADescription = 256
Global Const $MAX_WSASYSStatus = 128

Global Const $ICMP_OPTIONS = _
        "byte Ttl;" & _
        "byte Tos;" & _
        "byte Flags;" & _
        "byte OptionsSize;" & _
        "ulong OptionsData" ; Options Data

Global Const $tagICMP_ECHO_REPLY = _
        "ulong Address;" & _ ; IPAddr
        "ulong Status;" & _
        "ULONG RoundTripTime;" & _
        "USHORT DataSize;" & _
        "USHORT Reserved;" & _
        "ulong Data;" & _
        $ICMP_OPTIONS


; $ECHO receives an ICMP_ECHO_REPLY on success
; by Prog@ndy, used VBSource from http://vbnet.mvps.org/index.html?code/internet/ping.htm
; on success return 1 , else 0
Func _Ping($sAddress, $sDataToSend, ByRef $ECHO, $PING_TIMEOUT = 5000) ; ECHO As ICMP_ECHO_REPLY
;~   'If Ping succeeds :
;~   '.RoundTripTime = time in ms for the ping to complete,
;~   '.Data is the data returned (NULL terminated)
;~   '.Address is the Ip address that actually replied
;~   '.DataSize is the size of the string in .Data
;~   '.Status will be 0
;~   '
;~   'If Ping fails .Status will be the error code
    Local Static $wsock32 = DllOpen("wsock32.dll")

    Local Static $ICMPDLL = -1
    If $ICMPDLL = -1 Then
        If @OSVersion = "WIN_2000" Then
            $ICMPDLL = DllOpen("icmp.dll")
        Else
            $ICMPDLL = DllOpen("iphlpapi.dll")
        EndIf
    EndIf
;~    Local $ICMP = DllOpen("Iphlpapi.dll")
    Local $hPort ;As Long
    Local $dwAddress ;As Long

    If Not StringRegExp($sAddress, "\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}") Then
        TCPStartup()
        $sAddress = TCPNameToIP($sAddress)
        TCPShutdown()
    EndIf
;~   'convert the address into a long representation
    $dwAddress = DllCall($wsock32, "uint", "inet_addr", "str", $sAddress)
    If @error Then Return SetError($INADDR_NONE, 0, 0)
    $dwAddress = $dwAddress[0]
;~   'if a valid address..
    If $dwAddress = 4294967295 And $sAddress <> "255.255.255.255" Then Return SetError($INADDR_NONE, 0, 0)

;~      'open a port
    $hPort = DllCall($ICMPDLL, "hwnd", "IcmpCreateFile")
    If @error Then Return SetError($IP_NO_RESOURCES, 0, 0)
    $hPort = $hPort[0]

    Local $len
    If IsBinary($sDataToSend) Then
        $len = BinaryLen($sDataToSend)
    Else
        $len = StringLen($sDataToSend)
    EndIf
;~      'and if successful,
    If Not $hPort Then Return SetError($IP_NO_RESOURCES, 0, 0)
    $ECHO = DllStructCreate($tagICMP_ECHO_REPLY & ";byte[" & $len & "]")

;~         'ping it.
    Local $ret = _IcmpSendEcho($hPort, _
            $dwAddress, _
            $sDataToSend, _
            $len, _
            0, _
            DllStructGetPtr($ECHO), _
            DllStructGetSize($ECHO), _
            $PING_TIMEOUT, _
            $ICMPDLL)

;~         'return the status as ping succes and close
    Local $return = 0
    Local $error = DllStructGetData($ECHO, "Status")
    If @error Then $error = $IP_NO_RESOURCES
    If $error = $IP_SUCCESS Then $return = 1

    DllCall($ICMPDLL, "uint", "IcmpCloseHandle", "hwnd", $hPort)


    Return SetError($error, 0, $return)
EndFunc   ;==>_Ping

; by BugFix, modified by Prog@ndy
; für 1000 < @error < 1004 is der error von Dllcall. Die DllCall-Fehlernummer ist dabei @error/1000
Func _IcmpSendEcho($IcmpHandle, $DestinationAddress, $RequestData, $RequestSize, $RequestOptions, $ReplyBuffer, $ReplySize, $Timeout, $ICMPDLL = "icmp.dll")
    Local $sType = "str"
    If IsBinary($RequestData) Then
        $sType = "ptr"
        Local $tS = DllStructCreate("byte[" & $RequestSize & ']')
        DllStructSetData($tS, 1, $RequestData)
        $RequestData = DllStructGetPtr($tS)
    EndIf
    Local $ret = DllCall($ICMPDLL, "dword", "IcmpSendEcho", _
            "hwnd", $IcmpHandle, _
            "uint", $DestinationAddress, _
            $sType, $RequestData, _
            "dword", $RequestSize, _
            "ptr", $RequestOptions, _
            "ptr", $ReplyBuffer, _
            "dword", $ReplySize, _
            "dword", $Timeout)
    If @error Then Return SetError(@error + 1000, 0, 0)
    Return $ret[0]
EndFunc   ;==>_IcmpSendEcho

Func _DecIPToString($DecIP)
    Local $IPString = DllCall("ws2_32.dll", "str", "inet_ntoa", "uint", $DecIP)
    If @error Then Return SetError(1, "0.0.0.0")
    Return $IPString[0]
EndFunc   ;==>_DecIPToString

Example:

; --- EXAMPLE ---

Local $ECHORet

Local $pingSucess = _Ping("autoit.de", "The MSG", $ECHORet)
$error = @error
$returnedText = DllStructCreate("char[" & DllStructGetData($ECHORet, "DataSize") & "]", DllStructGetData($ECHORet, "Data"))
MsgBox(0, 'Ping autoit.de', "Ping successful:     " & ($pingSucess = 1) & " (@error: " & $error & ")" & @CRLF & _
        "Target-IP:         " & _DecIPToString(DllStructGetData($ECHORet, "Address")) & @CRLF & _
        "Ping time:         " & DllStructGetData($ECHORet, "RoundTripTime") & " ms" & @CRLF & _
        "Set data (String):     " & DllStructGetData($returnedText, 1))

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Link to comment
Share on other sites

This is something I wrote in 2008 (just made some improvements now).

#include <WinAPI.au3>

Global Const $IP_SUCCESS = 0
Global Const $IP_STATUS_BASE = 11000
Global Const $IP_BUF_TOO_SMALL = ($IP_STATUS_BASE + 1)
Global Const $IP_DEST_NET_UNREACHABLE = ($IP_STATUS_BASE + 2)
Global Const $IP_DEST_HOST_UNREACHABLE = ($IP_STATUS_BASE + 3)
Global Const $IP_DEST_PROT_UNREACHABLE = ($IP_STATUS_BASE + 4)
Global Const $IP_DEST_PORT_UNREACHABLE = ($IP_STATUS_BASE + 5)
Global Const $IP_NO_RESOURCES = ($IP_STATUS_BASE + 6)
Global Const $IP_BAD_OPTION = ($IP_STATUS_BASE + 7)
Global Const $IP_HW_ERROR = ($IP_STATUS_BASE + 8)
Global Const $IP_PACKET_TOO_BIG = ($IP_STATUS_BASE + 9)
Global Const $IP_REQ_TIMED_OUT = ($IP_STATUS_BASE + 10)
Global Const $IP_BAD_REQ = ($IP_STATUS_BASE + 11)
Global Const $IP_BAD_ROUTE = ($IP_STATUS_BASE + 12)
Global Const $IP_TTL_EXPIRED_TRANSIT = ($IP_STATUS_BASE + 13)
Global Const $IP_TTL_EXPIRED_REASSEM = ($IP_STATUS_BASE + 14)
Global Const $IP_PARAM_PROBLEM = ($IP_STATUS_BASE + 15)
Global Const $IP_SOURCE_QUENCH = ($IP_STATUS_BASE + 16)
Global Const $IP_OPTION_TOO_BIG = ($IP_STATUS_BASE + 17)
Global Const $IP_BAD_DESTINATION = ($IP_STATUS_BASE + 18)
Global Const $IP_ADDR_DELETED = ($IP_STATUS_BASE + 19)
Global Const $IP_SPEC_MTU_CHANGE = ($IP_STATUS_BASE + 20)
Global Const $IP_MTU_CHANGE = ($IP_STATUS_BASE + 21)
Global Const $IP_UNLOAD = ($IP_STATUS_BASE + 22)
Global Const $IP_ADDR_ADDED = ($IP_STATUS_BASE + 23)
Global Const $IP_GENERAL_FAILURE = ($IP_STATUS_BASE + 50)
Global Const $MAX_IP_STATUS = ($IP_STATUS_BASE + 50)
Global Const $IP_PENDING = ($IP_STATUS_BASE + 255)
Global Const $PING_TIMEOUT = 500
Global Const $WS_VERSION_REQD = 0x101
Global Const $MIN_SOCKETS_REQD = 1
Global Const $SOCKET_ERROR = -1
Global Const $INADDR_NONE = 0xFFFFFFFF
Global Const $MAX_WSADescription = 256
Global Const $MAX_WSASYSStatus = 128

Global Const $ICMP_OPTIONS = _
        "byte Ttl;" & _
        "byte Tos;" & _
        "byte Flags;" & _
        "byte OptionsSize;" & _
        "ulong OptionsData" ; Options Data

Global Const $tagICMP_ECHO_REPLY = _
        "ulong Address;" & _ ; IPAddr
        "ulong Status;" & _
        "ULONG RoundTripTime;" & _
        "USHORT DataSize;" & _
        "USHORT Reserved;" & _
        "ulong Data;" & _
        $ICMP_OPTIONS


; $ECHO receives an ICMP_ECHO_REPLY on success
; by Prog@ndy, used VBSource from http://vbnet.mvps.org/index.html?code/internet/ping.htm
; on success return 1 , else 0
Func _Ping($sAddress, $sDataToSend, ByRef $ECHO, $PING_TIMEOUT = 5000) ; ECHO As ICMP_ECHO_REPLY
;~   'If Ping succeeds :
;~   '.RoundTripTime = time in ms for the ping to complete,
;~   '.Data is the data returned (NULL terminated)
;~   '.Address is the Ip address that actually replied
;~   '.DataSize is the size of the string in .Data
;~   '.Status will be 0
;~   '
;~   'If Ping fails .Status will be the error code
    Local Static $wsock32 = DllOpen("wsock32.dll")

    Local Static $ICMPDLL = -1
    If $ICMPDLL = -1 Then
        If @OSVersion = "WIN_2000" Then
            $ICMPDLL = DllOpen("icmp.dll")
        Else
            $ICMPDLL = DllOpen("iphlpapi.dll")
        EndIf
    EndIf
;~    Local $ICMP = DllOpen("Iphlpapi.dll")
    Local $hPort ;As Long
    Local $dwAddress ;As Long

    If Not StringRegExp($sAddress, "\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}") Then
        TCPStartup()
        $sAddress = TCPNameToIP($sAddress)
        TCPShutdown()
    EndIf
;~   'convert the address into a long representation
    $dwAddress = DllCall($wsock32, "uint", "inet_addr", "str", $sAddress)
    If @error Then Return SetError($INADDR_NONE, 0, 0)
    $dwAddress = $dwAddress[0]
;~   'if a valid address..
    If $dwAddress = 4294967295 And $sAddress <> "255.255.255.255" Then Return SetError($INADDR_NONE, 0, 0)

;~      'open a port
    $hPort = DllCall($ICMPDLL, "hwnd", "IcmpCreateFile")
    If @error Then Return SetError($IP_NO_RESOURCES, 0, 0)
    $hPort = $hPort[0]

    Local $len
    If IsBinary($sDataToSend) Then
        $len = BinaryLen($sDataToSend)
    Else
        $len = StringLen($sDataToSend)
    EndIf
;~      'and if successful,
    If Not $hPort Then Return SetError($IP_NO_RESOURCES, 0, 0)
    $ECHO = DllStructCreate($tagICMP_ECHO_REPLY & ";byte[" & $len & "]")

;~         'ping it.
    Local $ret = _IcmpSendEcho($hPort, _
            $dwAddress, _
            $sDataToSend, _
            $len, _
            0, _
            DllStructGetPtr($ECHO), _
            DllStructGetSize($ECHO), _
            $PING_TIMEOUT, _
            $ICMPDLL)

;~         'return the status as ping succes and close
    Local $return = 0
    Local $error = DllStructGetData($ECHO, "Status")
    If @error Then $error = $IP_NO_RESOURCES
    If $error = $IP_SUCCESS Then $return = 1

    DllCall($ICMPDLL, "uint", "IcmpCloseHandle", "hwnd", $hPort)


    Return SetError($error, 0, $return)
EndFunc   ;==>_Ping

; by BugFix, modified by Prog@ndy
; für 1000 < @error < 1004 is der error von Dllcall. Die DllCall-Fehlernummer ist dabei @error/1000
Func _IcmpSendEcho($IcmpHandle, $DestinationAddress, $RequestData, $RequestSize, $RequestOptions, $ReplyBuffer, $ReplySize, $Timeout, $ICMPDLL = "icmp.dll")
    Local $sType = "str"
    If IsBinary($RequestData) Then
        $sType = "ptr"
        Local $tS = DllStructCreate("byte[" & $RequestSize & ']')
        DllStructSetData($tS, 1, $RequestData)
        $RequestData = DllStructGetPtr($tS)
    EndIf
    Local $ret = DllCall($ICMPDLL, "dword", "IcmpSendEcho", _
            "hwnd", $IcmpHandle, _
            "uint", $DestinationAddress, _
            $sType, $RequestData, _
            "dword", $RequestSize, _
            "ptr", $RequestOptions, _
            "ptr", $ReplyBuffer, _
            "dword", $ReplySize, _
            "dword", $Timeout)
    If @error Then Return SetError(@error + 1000, 0, 0)
    Return $ret[0]
EndFunc   ;==>_IcmpSendEcho

Func _DecIPToString($DecIP)
    Local $IPString = DllCall("ws2_32.dll", "str", "inet_ntoa", "uint", $DecIP)
    If @error Then Return SetError(1, "0.0.0.0")
    Return $IPString[0]
EndFunc   ;==>_DecIPToString

Example:

; --- EXAMPLE ---

Local $ECHORet

Local $pingSucess = _Ping("autoit.de", "The MSG", $ECHORet)
$error = @error
$returnedText = DllStructCreate("char[" & DllStructGetData($ECHORet, "DataSize") & "]", DllStructGetData($ECHORet, "Data"))
MsgBox(0, 'Ping autoit.de', "Ping successful:     " & ($pingSucess = 1) & " (@error: " & $error & ")" & @CRLF & _
        "Target-IP:         " & _DecIPToString(DllStructGetData($ECHORet, "Address")) & @CRLF & _
        "Ping time:         " & DllStructGetData($ECHORet, "RoundTripTime") & " ms" & @CRLF & _
        "Set data (String):     " & DllStructGetData($returnedText, 1))

Thanks!!!

The script now accept by _ping function the Data Size parameters.

You are a boss! ;)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...