Entwickler-Ecke

Internet / Netzwerk - Pingen


Anonymous - Mi 06.08.03 17:41
Titel: Pingen
Wie kann ich am einfachsten einen Rechner anpingen, damit ich sehe ob er im Netzwerk ist....


Bitte mit Code...Kann mit den Beispielen leider nichts anfangen....


Gruß

Alex


MSCH - Mi 06.08.03 19:36

sofern du die Indy-Kompos installiert hast via:


Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
  function isHostAvailable(Host:String):boolean;
  var
     MS: Integer;
    ICMP: TidIcmpClient;
  begin
    ICMP:= TidICMPClient.Create(nil);
    result:= False;
    ICMP.Host:= host;
    ICMP.Ping;
    MS:=ICMP.ReplyStatus.MsRoundTripTime;
    result:= ICMP.ReplyStatus.ReplyStatusType <> rsTimeOut;
    ICMP.Free;
  end;


MS ist Integer und gibt die zeit zurück

grez
msch


Tino - Do 07.08.03 10:33

Hallo!

Ich habs wie folgt gelöst:

Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
Type
  tIP = Record
      V1,
      V2,
      V3,
      V4: Byte;
    End;

  IPAddr = DWORD;

  PICMP_ECHO_REPLY = ^ICMP_ECHO_REPLY;
  ICMP_ECHO_REPLY = packed record
    Address       : ULONG;
    Status        : ULONG;
    RoundTripTime : ULONG;
    DataSize      : WORD;
    Reserved      : WORD;
    Data          : Pointer;
  end;

  PIP_OPTION_INFORMATION = ^IP_OPTION_INFORMATION;
  IP_OPTION_INFORMATION = packed record
    Ttl         : byte;
    Tos         : byte;
    Flags       : byte;
    OptionsSize : byte;
    OptionsData : Pointer;
  end;

function IcmpCreateFile : DWORD; stdcallexternal 'icmp.dll';

function IcmpCloseHandle (
    const IcmpHandle : DWORD
  ) : longbool; stdcallexternal 'icmp.dll';

function IcmpSendEcho (
    const IcmpHandle : DWORD;
    const DestinationAddress : IPAddr;
    const RequestData : Pointer;
    const RequestSize : WORD;
    const RequestOptions : PIP_OPTION_INFORMATION;
    const ReplyBuffer : Pointer;
    const ReplySize : DWORD;
    const TimeOut : DWORD
  ) : DWORD; stdcallexternal 'icmp.dll';

Function SendPing (aIP: tIP): Boolean;
Var
  hICMP    : DWORD;
  pierWork : PICMP_ECHO_REPLY;
  dwSize   : DWORD;
Begin
  hICMP := IcmpCreateFile;
  If hICMP = INVALID_HANDLE_VALUE Then
    Exit;

  Try
    dwSize   := SizeOf (ICMP_ECHO_REPLY) +8;
    pierWork := AllocMem (dwSize);
    Try
      SendPing := IcmpSendEcho (
          hICMP,
          MAKELONG (MAKEWORD (aIP.V1, aIP.V2), MAKEWORD (aIP.V3, aIP.V4)),
          nil0nil, pierWork, dwSize, 50) <> 0;
    Finally
      FreeMem (pierWork,dwSize);
    End;
  Finally
    IcmpCloseHandle (hIcmp);
  End;
End;

Gruß
Tino


kasi - Do 07.08.03 15:30

Ein paar Ergänzungen waren bei mir nötig um Tino's Beispiel zum laufen zu bringen.

Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
Type
  tIP = Record 
      V1, 
      V2, 
      V3, 
      V4: Byte; 
    End

  pOptionInfo = ^TOptionInfo;
  TOptionInfo = Packed Record
     TimeToLive  : Byte;
     TypeService : Byte;
     HdrFlags    : Byte;
     OptionDataSize : Byte;
     OptionsData : PChar;
  End;

  PICMP_ECHO_REPLY = ^ICMP_ECHO_REPLY;
  ICMP_ECHO_REPLY = Packed Record
     ReplyAddr    : DWord;
     IPStatus     : DWord;
     RndTripTime  : DWord;
     ReplyDataSize: Word;
     Reserved     : Word;
     ReplyDataPtr : Pointer;
     ReplyOptions : TOptionInfo;    // Reply options
  End;

  TIcmpCreateFile  = function: THandle; stdcall;
  TIcmpSendEcho = Function (IcmpHandle:THandle; DestinationAddress : DWord; RequestData : Pointer;
                      RequestSize: Word; RequestOptions: POptionInfo; ReplyBuffer: Pointer;
                      ReplySize: DWord; Timeout: DWord ): DWord; stdcall;
  TIcmpCloseHandle = Function (IcmpHandle : THandle): Boolean; StdCall;

var
  IcmpCreateFile :  TIcmpCreateFile;
  IcmpCloseHandle : TIcmpCloseHandle;
  IcmpSendEcho :    TIcmpSendEcho;


......

Function SendPing (aIP: tIP): Boolean; 
Var 
  hICMP    : DWORD; 
  pierWork : PICMP_ECHO_REPLY; 
  dwSize   : DWORD; 
Begin
  Result := false;
  hICMP := IcmpCreateFile; 
  If hICMP = INVALID_HANDLE_VALUE Then 
    Exit; 

  Try 
    dwSize   := SizeOf (ICMP_ECHO_REPLY) +8
    pierWork := AllocMem (dwSize); 
    Try 
      SendPing := IcmpSendEcho ( 
          hICMP, 
          MAKELONG (MAKEWORD (aIP.V1, aIP.V2), MAKEWORD (aIP.V3, aIP.V4)), 
          nil0nil, pierWork, dwSize, 50) <> 0
    Finally 
      FreeMem (pierWork,dwSize); 
    End
  Finally 
    IcmpCloseHandle (hIcmp); 
  End
End;

procedure TForm1.Button2Click(Sender: TObject);
var
  IP : tIP;
  ICMPdllHandle : THandle;
  ICMPDLL: string;
  Buf: array[0..MAX_PATH] of Char;

begin
  GetSystemDirectory(@Buf, SizeOf(Buf));
  ICMPDLL := StrPas(Buf)+'\ICMP.DLL';
  IcmpDllHandle := LoadLibrary(PChar(ICMPDLL));
  if IcmpDllhandle <> INVALID_HANDLE_VALUE then
    try
    @ICMPCreateFile  := GetProcAddress(IcmpDllhandle, 'IcmpCreateFile');
    @IcmpCloseHandle := GetProcAddress(IcmpDllhandle, 'IcmpCloseHandle');
    @IcmpSendEcho := GetProcAddress(IcmpDllhandle, 'IcmpSendEcho');
    IP.V1 := StrToInt(Edit1.Text);
    IP.V2 := StrToInt(Edit2.Text);
    IP.V3 := StrToInt(Edit3.Text);
    IP.V4 := StrToInt(Edit4.Text);
    if SendPing(IP) then
      LabelPing.Caption := 'Ping erfolgreich'
    else
      LabelPing.Caption := 'Ping nicht erfolgreich';
    finally
      FreeLibrary(ICMPdllHandle);
    end;

end;


KASI

Moderiert von user profile iconTino: Code- durch Delphi-Tags ersetzt.


Tino - Fr 08.08.03 09:30

kasi hat folgendes geschrieben:
Ein paar Ergänzungen waren bei mir nötig um Tino's Beispiel zum laufen zu bringen.

Stimmt. Hab im Posting vergessen die Records zu definieren und die DLL-Funktionen zu importieren. Habs oben eingefügt.

Gruß
Tino


Yankee - Mi 20.08.03 09:28

Kann man auch nen "Ping Of Death" schicken. Oder eine Ping Flooding Atacke??? Würde mich mal interresieren!!


:twisted: :twisted: :twisted: :twisted: :twisted: :twisted:


Logi5 - Sa 30.08.03 14:43

@ MSCH

Zitat:
function isHostAvailable(Host:String):boolean;
var
MS: Integer;
ICMP: TidIcmpClient;
begin
ICMP:= TidICMPClient.Create(nil);
result:= False;
ICMP.Host:= host;
ICMP.Ping;
MS:=ICMP.ReplyStatus.MsRoundTripTime;
result:= ICMP.ReplyStatus.ReplyStatusType <> rsTimeOut;
ICMP.Free;
end;


leider geht es nicht! muss ich noch was in die Uses Abteilung einfügen? Wenn ja, was?

Fehlermeldung: Undefinierter Bezeichner = TidIcmpClient

Logi5


FeG - Sa 30.08.03 14:56

Zitat:
leider geht es nicht! muss ich noch was in die Uses Abteilung einfügen? Wenn ja, was?

Fehlermeldung: Undefinierter Bezeichner = TidIcmpClient

Siehst du doch schon an der Fehlermeldung, dass du was einfügen musst, nämlich IdICMPClient...

MfG,
FeG