Recent

Author Topic: Getting local IP addresses  (Read 788 times)

Prime

  • Jr. Member
  • **
  • Posts: 62
Getting local IP addresses
« on: March 13, 2023, 10:32:23 am »
Hi all,

I'm porting some code from Delphi 7 to Lazarus, on Delphi I use one for the Jv components, TJvComputerInfoEx, to get the local IP address e.g. the one assigned to the network card (or other link) in the machine.

Is there a (hopefully cross platform) way of doing this under Lazarus / FPC. Even separate methodsfor platforms might be OK as I can make a wrapper that presents them to the client code in a platform independent way.

Cheers.

Phill.

six1

  • Full Member
  • ***
  • Posts: 117
Re: Getting local IP addresses
« Reply #1 on: March 13, 2023, 10:56:34 am »
You can try this

Code: Pascal  [Select][+][-]
  1. var
  2.    txt:string;
  3. begin
  4. {$IFDEF Linux}
  5.   RunCommand('ip', ['a'], txt);
  6. {$ELSEIF Windows}
  7.   RunCommand('ipconfig', [''], txt);
  8. {$ENDIF}
  9.  showmessage (txt);
  10. end;                    
  11.  

and parse the output for your needs

Warfley

  • Hero Member
  • *****
  • Posts: 1499
Re: Getting local IP addresses
« Reply #2 on: March 13, 2023, 11:39:00 am »
The ip utils might not be installed on Linux.

To get the IP address without external tools you can simply read the pseudofiles in /proc/net:
Code: Text  [Select][+][-]
  1. ➜ cat /proc/net/if_inet6
  2. fe80000000000000021...095 06 40 20 80     eth0
  3. 00000000000000000000000000000001 01 80 10 80       lo
This file contains all the ipv6 interaces, eth0 with the IPv6 address fe80::21...095 (redacted for privacy reasons) and the loopback with ::1
« Last Edit: March 13, 2023, 11:41:30 am by Warfley »

 

TinyPortal © 2005-2018