PING mit spezieller Ping Payload

  • Hallo,
    ich bin neu im Forum und arbeite auch noch nicht lang mit AutoIt. Ich finde diese Programmiersprache aber super und habe auch bereits erste Programme erstellt.

    Aber jetzt zu meiner Frage: für die leichtere Analyse von aufgezeichneten Netzwerkdaten, möchte ich in den Daten kleine "Kennzeichen" setzen. Dies soll über ein PING mit speziellem Text als PING Payload gelöst werden. In VB habe ich auch einen entsprechenden Programmteil gefunden.
    Dort wird über die icmp.dll ein Objekt IcmpCreateFile() erstellt, was dann über IcmpSendEcho(...,PingPayload, ....) das PING mit den entsprechenden Daten (Text) sendet.
    Meine ersten Versuche mit DllOpen und DllCall brachten nur Fehler 1. Auch die PING Funktion von AautoIt dürfte hier nicht helfen. Aber vielleicht hat ja jemand im Forum eine zündende Idee. ;)

    Gruß Ingo

  • Hallo,
    hier mal einen Verweis auf einen solchen VB-Gode. Besonders interessiert mich natürlich die Funktion:
    Private Function Ping(sAddress As String, _sDataToSend As String, _ECHO As ICMP_ECHO_REPLY) As Long

    Gruß Ingo

  • Hallo,
    ich habe dam mal etwas zusammengestellt was allerdings nicht funktioniert.
    Ich vermute, dass der Aufruf von IcmpSendEcho nicht funktioniert weil das PING Ziel nicht als Datentyp „long“ vorliegt, sondern als Text. Auch $PING_Antwort kann eigentlich nicht die zurückgegebenen Werte aufnehmen. Könnte hier eventuell ein DllStructCreate helfen?

    Die Funktion IcmpSendEcho hat übrigens die folgende Deklaration:

    Code
    Declare Function MessageBox Lib "user32.dll" _
                     Alias "MessageBoxA" ( _
                     ByVal hwnd As Long, _
                     ByVal lpText As String, _
                     ByVal lpCaption As String, _
                     ByVal wType As Long) As Long

    Eventuell hat ja im Forum jemand einen Lösungsansatz für mich.

    Gruß Ingo

    Spoiler anzeigen
    [autoit]


    AutoIt Version: 3.2.12.1
    ;Platform: Windows XP

    [/autoit] [autoit][/autoit] [autoit]

    Ping_mit_Text ("172.25.0.10", "Test")

    [/autoit] [autoit][/autoit] [autoit]

    Func Ping_mit_Text ($PING_Ziel, $PING_Text = "")
    Local $ICMP_DLL
    Local $DLL_IcmpCreateFile
    Local $PING_Antwort

    $ICMP_DLL = DllOpen ("icmp.dll")
    If $ICMP_DLL = -1 Then
    MsgBox ( 0, "Fehler", "ICMP.dll konnte nicht geöffnet werden!")
    Exit (1)
    EndIf

    $DLL_IcmpCreateFile = DllCall ($ICMP_DLL, "long", "IcmpCreateFile")
    If $DLL_IcmpCreateFile = 0 Then
    MsgBox ( 0, "Fehler", "Unable to Create File Handle IcmpCreateFile")
    Exit (1)
    EndIf

    $PING_Antwort = DllCall ($ICMP_DLL, "long", "IcmpSendEcho", "long", $DLL_IcmpCreateFile, "long", $PING_Ziel)
    If $PING_Antwort = 0 Then
    MsgBox ( 0, "Fehler", "PING konnte nicht ausgeführt werden")
    Exit (1)
    EndIf

    DllClose ($ICMP_DLL)

    MsgBox ( 1, "Test", $PING_Antwort)
    EndFunc

    [/autoit]
    • Offizieller Beitrag

    Das Problem sind die (in AutoIt) nicht existierenden Datentypen, die dort verwendet werden.
    DllStructCreate() hilft möglicherweise. Aber es reicht ja nicht die Struktur zu erstellen, du mußt ja auch Daten in diese Struktur schreiben. Da fehlt mir momentan der Ansatz zu.

    Ich hab mal begonnen, das VB-Skript umzusetzen. Vielleicht hilft dir der Ansatz ja.

    Spoiler anzeigen
    [autoit]

    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

    [/autoit] [autoit][/autoit] [autoit]

    Global $ICMP_OPTIONS = DllStructCreate("byte Ttl;" & _
    "byte Tos;" & _
    "byte Flags;" & _
    "byte OptionsSize;" & _
    "long OptionsData")

    Global $ICMP_ECHO_REPLY = DllStructCreate("long Address;" & _
    "long status;" & _
    "long RoundTripTime;" & _
    "long DataSize;" & _
    "integer Reserved;" & _
    "long_ptr DataPointer;" & _
    $ICMP_OPTIONS & " Options;" & _
    "char Data[250]")

    Global $WSADATA = DllStructCreate("int wVersion;" & _
    "int wHighVersion;" & _
    "byte szDescription;" & _ ; ? szDescription(0 To MAX_WSADescription) As Byte
    "byte szSystemStatus;" & _ ; ? szSystemStatus(0 To MAX_WSASYSStatus) As Byte
    "long wMaxSockets;" & _
    "long wMaxUDPDG;" & _
    "long dwVendorInfo")

    [/autoit] [autoit][/autoit] [autoit]


    Func _IcmpSendEcho($IcmpHandle, $DestinationAddress, $RequestData, $RequestSize, $RequestOptions, $ReplyBuffer, $ReplySize, $Timeout)
    DllCall("icmp.dll", "long", "IcmpSendEcho", _
    "long", $IcmpHandle, _
    "long", $DestinationAddress, _
    "str", $RequestData, _
    "long", $RequestSize, _
    "long", $RequestOptions, _
    $ICMP_ECHO_REPLY, $ReplyBuffer, _
    "long", $ReplySize, _
    "long", $Timeout)
    EndFunc

    [/autoit]

    Aber wie gesagt, ist es mit dem Erstellen der Struktur nicht getan - an dieser Stelle komm ich nicht weiter. (Deshalb hab ich auch nur mal bis zur ersten Funktion portiert).

  • Hallo BugFix,
     das muss ich erst verdauen. Für mich als Anfänger ist das keine leiste Kost. Ich werde das mal in mein Programm einbauen und dann schauen was dabei raus kommt.
      
     Vielen Dank für deine Schnelle Hilfe!!!
      
     Gruß Ingo

  • PHP

    ich hab die _Ping funktion mal umgesetzt, aber ist gerade auf dem anderen PC, an den ich grad nicht ran kann. Werde sie dann mal Posten, wenn ich Zeit dazu habe :)
    /Edit: mache neuen Post

    2 Mal editiert, zuletzt von progandy (9. September 2008 um 19:21)

  • Die Funktion, wie sie geht (auf 32 Bit)

    Spoiler anzeigen
    [autoit]

    #include <WinAPI.au3>

    [/autoit] [autoit][/autoit] [autoit]

    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

    [/autoit] [autoit][/autoit] [autoit]

    If @AutoItX64 Then Exit 0 * MsgBox(16, @ScriptName & " - Error", "Structures only designed for 32-Bit Version")

    [/autoit] [autoit][/autoit] [autoit]

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

    [/autoit] [autoit][/autoit] [autoit]

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

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    ; $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
    Local $return = 0, $error = 0
    ;~ '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 $wsock32 = DllOpen("wsock32.dll")
    Local $ICMPDLL = DllOpen("icmp.dll")
    ;~Local $ICMP = DllOpen("Iphlpapi.dll")
    Local $hPort ;As Long
    Local $dwAddress ;As Long
    Local $INADDR_NONE = -1
    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)
    $dwAddress = $dwAddress[0]
    ;~ 'if a valid address..
    If $dwAddress <> $INADDR_NONE Or $sAddress = "255.255.255.255" Then

    [/autoit] [autoit][/autoit] [autoit]

    ;~ 'open a port
    $hPort = DllCall($ICMPDLL, "hwnd", "IcmpCreateFile")
    $hPort = $hPort[0]

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    ;~ 'and if successful,
    If $hPort Then
    $ECHO = DllStructCreate($tagICMP_ECHO_REPLY & ";char[355]")

    [/autoit] [autoit][/autoit] [autoit]

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

    [/autoit] [autoit][/autoit] [autoit]

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

    [/autoit] [autoit][/autoit] [autoit]

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

    [/autoit] [autoit][/autoit] [autoit]

    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    Else
    ;~ 'the address format was probably invalid
    $return = 0
    $error = $INADDR_NONE

    [/autoit] [autoit][/autoit] [autoit]

    EndIf
    DllClose($wsock32)
    DllClose($ICMPDLL)

    [/autoit] [autoit][/autoit] [autoit]

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

    [/autoit] [autoit][/autoit] [autoit]

    ; 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 $ret = DllCall($ICMPDLL, "dword", "IcmpSendEcho", _
    "hwnd", $IcmpHandle, _
    "uint", $DestinationAddress, _
    "str", $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

    [/autoit] [autoit][/autoit] [autoit]

    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

    [/autoit] [autoit][/autoit] [autoit]

    ;#################
    ;Example

    [/autoit] [autoit][/autoit] [autoit]

    Local $ECHORet

    [/autoit] [autoit][/autoit] [autoit]

    Local $pingSucess = _Ping("google.de", "The MSG", $ECHORet)
    $returnedText = DllStructCreate("char[" & DllStructGetData($ECHORet, "DataSize") & "]", DllStructGetData($ECHORet, "Data"))
    MsgBox(0, 'Ping zu google.de' , "Der Ping war erfolgreich: " & ($pingSucess=1) & @CRLF & _
    "Die Ziel-IP war: " & _DecIPToString(DllStructGetData($ECHORet, "Address")) & @CRLF & _
    "Die Ping-Time war: " & DllStructGetData($ECHORet, "RoundTripTime") & " ms" & @CRLF & _
    "Die gesendete Daten (String): " & DllStructGetData($returnedText, 1))

    [/autoit]
  • Hallo progandy,
     noch eine Frage: Welche Funktion haben eigentlich die oben definierten Konstanten? Außer $IP_SUCCESS mit dem Wert 0 und den beiden Strukturen wird keine im Programm verwendet. Wenn ich die Definitionen entferne funktioniert das Programm trotzdem.
     Oder habe ich da etwas übersehen? :(


     Gruß Ingo

  • Die Konstanten sind mögliche Fehlermeldungen , die ich in _Ping als SetError setze :) (werden von IcmpSendEcho in $tagICMP_ECHO_REPLY als Status gesetzt )