![]() |
IP Range
Ich habe zwei IP Adressen: 192.168.2.100 und 192.168.5.100 Jeder Teil der IP Adresse liegt mir als Integer vor. Wie kann ich jetzt alle IP Adressen in diesem Bereich generieren? Ich habe da irgendwie gerade einen Blackout. :?
|
Re: IP Range
Zitat:
Delphi-Quellcode:
TIP = array[0..3] of Integer;
function makeip(IP: Integer): String; begin result := IntToStr((IP shr 24) and $FF) + '.' + IntToStr((IP shr 16) and $FF) + '.' + IntToStr((IP shr 8) and $FF) + '.' + IntToStr((IP shr 0) and $FF); end; ip1_int := ((ip1[0] and $FF) shl 24) or ((ip1[1] and $FF) shl 16) or ((ip1[2] and $FF) shl 8) or ((ip1[3] and $FF) shl 0); ip2_int := ((ip2[0] and $FF) shl 24) or ((ip2[1] and $FF) shl 16) or ((ip2[2] and $FF) shl 8) or ((ip2[3] and $FF) shl 0); for i := ip1_int to ip2_int do begin Memo1.Lines.Add(IntToStr(makeip(i)); end; |
Re: IP Range
Mann bei dem ganzen rumgeschiebe wird einem ja schwindelig. Aber es geht. Ich glaube, ich hätte da was mit zeichenketten Routinen rumgebastelt. ;) Besten Dank.
|
Re: IP Range
Zitat:
bei ipv6 hättest du schon mehr probleme denn da bräuchtest du erstmal einen 128bit datentyp ;-) |
Re: IP Range
argh. mit fällt grad ein daß der Gebrauch von Integer als datentyp hier zu problemen führen kann. nimm lieber Cardinal/LongWord. aber ansonsten bleibt der code gleich.
|
Re: IP Range
Auch wenn ich es als LongWord deklariere, bekomme ich bei 192.168.2.100 - 255.255.255.255 einen "Range check error".
|
Re: IP Range
Hallo,
wie wär's hiermit:
Delphi-Quellcode:
Anwendung:
type
TIPAddr = array[0..3] of Byte; function GetNext(var IPAddr: TIPAddr): Boolean; var C: Integer; begin Result := True; for C := 3 downto 0 do begin if IPAddr[C] < 255 then begin Inc(IPAddr[C]); Exit; end; IPAddr[C] := 0; end; Result := False; end; function IsBelowOrEqual(IP, Limit: TIPAddr): Boolean; begin Result := (IP[0] shl 24 + IP[1] shl 16 + IP[2] shl 8 + IP[3]) <= (Limit[0] shl 24 + Limit[1] shl 16 + Limit[2] shl 8 + Limit[3]); end;
Delphi-Quellcode:
Gruß
procedure TForm1.Button1Click(Sender: TObject);
var Start, Stop: TIPAddr; begin Start[0] := 192; Start[1] := 168; Start[2] := 0; start[3] := 1; Stop[0] := 192; Stop[1] := 168; Stop[2] := 1; Stop[3] := 10; repeat ListBox1.Items.Add(IntToStr(Start[0]) + '.' + IntToStr(Start[1]) + '.' + IntToStr(Start[2]) + '.' + IntToStr(Start[3])); if not GetNext(Start) then Exit; until not IsBelowOrEqual(Start, Stop); end; xaromz |
Re: IP Range
Hallo mein Lieber ;-)
Hätte nicht gedacht, das auch ich Dir mal weiter helfen kann. Also zuerst die Integer-Werte wandeln is klar denke ich dann kannst du die CreateIPPool benutzen. Wichtig zu wissen: Die CreateIPPool war die Beste von 3 Versuchen mit dem besten zeitlichen Ergebenissen. Von ?.0.0.0 ?.50.255.255 benötigt sie ca 3-4 sek. Je nach CPU. Die anderen Versuche kommen dabei immer so um die 10-15 Sek weg. Villeicht bekommst Du sie ja noch schneller :-). In dem Fall lass von Dir hören.
Delphi-Quellcode:
type
PIp = record p1 : String; P2 : String; p3 : String; p4 : String; end; type MyIP = packed record case Byte of 0: (D,C,B,A : Byte); 1: (dwValue : DWord); end; function SplittIP(IP: string): PIp; var I: Integer; begin Result.p1 := ''; Result.p2 := ''; Result.p3 := ''; Result.p4 := ''; I := 0; repeat Inc(I, 1); if IP[I] <> '.' then Result.p1 := Result.p1+IP[I]; until IP[I] = '.'; repeat Inc(I, 1); if IP[I] <> '.' then Result.p2 := Result.p2+IP[I]; until IP[I] = '.'; repeat Inc(I, 1); if IP[I] <> '.' then Result.p3 := Result.p3+IP[I]; until IP[I] = '.'; repeat Inc(I, 1); if IP[I] <> '.' then Result.p4 := Result.p4+IP[I]; until I = Length(IP); end; function CreateIPPool(sStartIp, sEndIp: String): TStringList; var FromIP, ToIP, IP: MyIP; I: Integer; Adress: PIp; begin Result := TStringList.Create; Adress := SplittIP(sStartIp); with FromIP do begin A := StrToInt(Adress.p1); B := StrToInt(Adress.p2); C := StrToInt(Adress.p3); D := StrToInt(Adress.p4); end; Adress := SplittIP(sEndIp); with ToIP do begin A := StrToInt(Adress.p1); B := StrToInt(Adress.p2); C := StrToInt(Adress.p3); D := StrToInt(Adress.p4); end; for I := FromIP.dwValue to ToIP.dwValue do begin IP.dwValue := I; with IP do Result.Add(Format('%d.%d.%d.%d',[A,B,C,D])); end; end; |
Re: IP Range
man kann es auch kompiliziert machen. Warum addiert ihr nicht gleich mit den Strings ? Ist doch bestimmt kein Aufwand mit IP in Strings zu rechnen ;) (sarkasmus off)
Gruß Hagen
Delphi-Quellcode:
function ToIP(I1,I2,I3,I4: Integer): Cardinal;
function Check(Value: Integer): Byte; begin if (Value >= 0) and (Value <= 255) then Result := Value else raise Exception.Create('ToIP: Values I1,I2,I3,I4 must be in Range 0 upto 255 each'); end; begin Result := Check(I1) shl 24 or Check(I2) shl 16 or Check(I3) shl 8 or Check(I4); end; function IPToStr(Value: Cardinal): String; begin Result := Format('%d.%d.%d.%d', [Value shr 24, Value shr 16 and $FF, Value shr 8 and $FF, Value and $FF]); end; procedure Test; var IP: Cardinal; begin for IP := ToIP(192,168,2,100) to ToIP(192,168,5,100) do Writeln( IPToStr(IP) ); end; |
Re: IP Range
Weil es zulangsam ist!!
Habs grad mal getest: ?.0.0.0 ?.50.255.255: nach über 4 Minuten CPU-Zeit habe ich den das Programm zurückgesetzt... |
Alle Zeitangaben in WEZ +1. Es ist jetzt 01:25 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz