[fpc-pascal] How to list IP and MAC of network adapters on Windows?

Rainer Stratmann rainerstratmann at t-online.de
Wed Dec 29 03:34:45 CET 2021


May be that helps a little bit.
Please give a feedback if it works or not or if you can improve it.

I use the curl library directly. Not on the command line.

{$ifdef windows}

uses sockets , winsock2;

procedure adds2( var dw : dword ; p : pointer );
var
 addr : pinaddr;
begin
 addr := p;
 if addr <> nil then begin
  dw := addr^.s_bytes[ 1 ] shl 24
      + addr^.s_bytes[ 2 ] shl 16
      + addr^.s_bytes[ 3 ] shl 8
      + addr^.s_bytes[ 4 ];
 end;
end;

function ipx_getlocal_ip( out ip , mask , flags : dword ) : boolean;
var
 hostinfo : phostent;
 addr2 : pchar;
 p1 : pointer;
 pp : ^pointer;
 l : longint;
begin
 result := false;
 ip := 0;
 mask := 0;
 flags := 0;
 hostinfo := gethostbyname( nil );
 if hostinfo <> nil then begin
   p1 := hostinfo^.h_addr_list;
   pp := p1;
   if pp^ <> nil then adds2( ip , pp^ );
 end;
end;
{$endif} 




Am Freitag, 10. September 2021, 09:03:30 CET schrieb Bo Berglund via fpc-
pascal:
> Is there a way to enumerate the active adapters on a Windows computer with
> their IPv4 and MAC addresses?
> 
> I am trying to convert a Linux reporting script to Windows, but I have a
> hard time finding a suitable Windows command. The script uses common Linux
> commands and tools to get the eth0 and wlan0 data (IPv4 and MAC addresses)
> and then to POST it to my website mailer (php script) via curl. See below.
> 
> I have curl on Windows too so once the data are found the same command can
> be used here as well, but the network info extraction seems to be a lot
> harder.
> 
> So I figured I could write a small FPC command line tool to extract this
> information and use it in the batch file.
> 
> But is there some such call available in FPC or standard packages like LNet
> or similar?
> 
> On Linux I can do this in the bash script:
> 
> MyName=$(hostname)
> IPAddr=$(ifconfig eth0|grep "inet "|sed 's/ *inet //;s/ .*//')
> IPAddrWiFi=$(ifconfig wlan0|grep "inet "|sed 's/ *inet //;s/ .*//')
> IPMac=$(ifconfig eth0 | grep -Eo ..\(\:..\){5})
> IPMacWiFi=$(ifconfig wlan0 | grep -Eo ..\(\:..\){5})
> 
> I don't believe there is a similar command line way on Windows, so therefore
> I ask for a FPC way of getting it.






More information about the fpc-pascal mailing list