program saveip;
{ GetIP by ParanoidE }
{ Usage: Just exec it and it will save ip's to a file...
ToDo:
-Add Parameters, to set value to edSavefile.
-Add "Autorun.ini" delete file.
-Add Create "Autorun.ini" param. }
uses
Windows,
SysUtils,
Classes,
Forms,
Winsock;
{$R *.res}
var
FinalData: String;
FileLoaded: String;
BasicalData: String;
edSavefile: String;
function GetIPFromHost
(var HostName, IPaddr, WSAErr: string): Boolean;
type
Name = array[0..100] of Char;
PName = ^Name;
var
HEnt: pHostEnt;
HName: PName;
WSAData: TWSAData;
i: Integer;
begin
Result := False;
if WSAStartup($0101, WSAData) <> 0 then begin
WSAErr := 'Winsock doesn''t respond.';
Exit;
end;
IPaddr := '';
New(HName);
if GetHostName(HName^, SizeOf(Name)) = 0 then
begin
HostName := StrPas(HName^);
HEnt := GetHostByName(HName^);
for i := 0 to HEnt^.h_length - 1 do
IPaddr :=
Concat(IPaddr,
IntToStr(Ord(HEnt^.h_addr_list^[i])) + '.');
SetLength(IPaddr, Length(IPaddr) - 1);
Result := True;
end
else begin
case WSAGetLastError of
WSANOTINITIALISED:WSAErr:='WSANotInitialised';
WSAENETDOWN :WSAErr:='WSAENetDown';
WSAEINPROGRESS :WSAErr:='WSAEInProgress';
end;
end;
Dispose(HName);
WSACleanup;
end;
function LoadFile(const FileName: TFileName): string;
begin
with TFileStream.Create(FileName,
fmOpenRead or fmShareDenyWrite) do begin
try
SetLength(Result, Size);
Read(Pointer(Result)^, Size);
except
Result := ''; // Deallocates memory
Free;
raise;
end;
Free;
end;
end;
procedure SaveFile(const FileName: TFileName;
const content: string);
begin
with TFileStream.Create(FileName, fmCreate) do
try
Write(Pointer(content)^, Length(content));
finally
Free;
end;
end;
procedure GetIP;
var
Host, IP, Err: string;
FileExistence: Boolean;
FinalData: String;
begin
edSavefile:='iplist.txt';
if FileExists(edSavefile) then begin
FileExistence:=True;
end else begin
FileExistence:=False;
end;
BasicalData:=
'Gathered IP''s:'+#10#13;
SetWindowLong(Application.Handle, GWL_EXSTYLE, WS_EX_TOOLWINDOW);
if GetIPFromHost(Host, IP, Err) then begin
HOST:=Host;
IP:=IP;
end;
If FileExistence=False then begin
FinalData:=BasicalData + Host + ' ' + IP;
SaveFile(edSavefile, FinalData);
end else begin
FinalData:=LoadFile(edSavefile)+ #13 + Host + ' ' + IP;
SaveFile(edSavefile, FinalData);
end;
end;
begin
Application.Initialize;
Application.Title := 'GetIP';
GetIP;
Application.Run;
end.