Lokale IP-Adresse ermitteln

Hallo Sachverständige,

gibt es eine Methode, die lokale IP-Adresse des Rechners zu ermitteln und in einem Script weiterzuverarbeiten? Gerne auch über Shell-Befehle.

Hi Tondose!
could be difficult depending on how many ip adresses you have :slight_smile:

for /f "tokens=2 delims=:" %A in ('ipconfig ^| findstr "IPv4"') do @echo %A | findstr /v "^$"

:heart_eyes: :heart_eyes: :heart_eyes:

Thank you so much! Works great!

1 Like

Alas, I am not able to process the result in mAirList script. A syntax like

var
  IP: string;
begin
  IP := ShellExecute(for, /f "tokens=2 delims=:" %A in ('ipconfig ^| findstr "IPv4"') do @echo %A | findstr /v "^$");
end.

will leave a syntax error. Maybe I went about too naively …?

Maybe? I see that your IP variable is expecting a String, but the command entered could return a list of more than one IP. hmm, I’m on the wrong computer, but can try later :slight_smile:

Ececuted in cmd.exe It does return a single local IP adress, indeed …

For me it returns all valid IPV4 addresses. My local IP, Tailscale IP etc… =)

Oh, forgot the single quotes. Now I’m getting a type mismatch like you do.

Got it working… with a not so neat workaround… whatever works… i guess?

{CCE8ABBA-B60E-4A37-AB6F-4481ACCD3951}

ipconfig.mls

procedure OnLoad;
var
  IPAddress, FilePath: string;
  sl: TStringList;
begin
  // Define the path to the batch script and text file
  FilePath := 'C:\mAirlist\Instances\03-Thomas TEst\script\ip.txt';

  // Execute the batch file to generate the IP file
  ShellExecute('C:\mAirlist\Instances\03-Thomas TEst\script\ip.bat','');

  
  sl := TStringList.Create;
  try
    sl.LoadFromFile(FilePath);
    SystemLog(sl[0]);
  finally
    sl.Free;
  end;

end;

begin
end.

ip.bat

@echo off
for /f "tokens=14 delims= " %%i in ('ipconfig ^| findstr /i "IPv4"') do echo %%i > C:\mAirlist\Instances\03-Thomas TEst\script\ip.txt

Hmmm … For me this writes a sole . into ip.txt only (and thus triggers a List index out of bounds (0) error subsequently).

It would be nice to know if we can get any output back from Shellexecute intop mairlist script.

All the cmd commands above deliver a space before the IP Address.
I found one here, that seemst to work very well.

for /f "tokens=14" %%a in ('ipconfig ^| findstr IPv4') do set _IPaddr=%%a
echo IP is: %_IPaddr%

or as a single liner, if we somehow can get back the result into mAirlist script.

for /f "tokens=14" %a in ('ipconfig ^| findstr IPv4') do @echo %a
1 Like