Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2021-09

How to list IP and MAC of network adapters on Windows?

10 messages · 7 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Bo Berglund

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.

Bo Berglund Developer in Sweden

Reply by email

Sven Barth
to Bo Berglund

Bo Berglund via fpc-pascal schrieb am
Fr., 10. Sep. 2021, 09:41:

Is there a way to enumerate the active adapters on a Windows computer with their IPv4 and MAC addresses?

There is an API for that, but I don't remember that right now.

Show quoted text 8 lines
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.

You can use the ipconfig command and parse its output (especially ipconfig /all).

Regards,
Sven

Reply by email

Michael Van Canneyt
to Sven Barth
Sven Barth wrote 9 lines
Bo Berglund via fpc-pascal schrieb am Fr., 10. Sep. 2021, 09:41: > Is there a way to enumerate the active adapters on a Windows computer with > their > IPv4 and MAC addresses? > There is an API for that, but I don't remember that right now.

Windows:

uses {$IFDEF MSWINDOWS}Windows, WinSock,{$ENDIF}

type
   pu_long = ^U_long;
var
   HostEnt : PHostEnt;
   InAddr : winsock.TInAddr;
   namebuf : Array[0..255] of ansichar;
   GinitData: TWSAData;
begin
   Result:='';
   WSAStartup($101, GInitData);
   Try
     GetHostName(namebuf,sizeof(namebuf));
     HostEnt := gethostbyname(namebuf);
     if (HostEntNil) then
      begin
      InAddr.S_addr := u_long(pu_long(HostEnt^.h_addr_list^)^);
      Result := inet_ntoa(InAddr);
      end;
   Finally
     WSACleanup;
   End;
end;

Michael.

Reply by email

Michael Van Canneyt
to James Richters
James Richters wrote
Well, that sure is a lot simpler than parsing the IPConfig output! Is there a way to capture the MAC address this way as well?

I don't know, I never needed that.

uses {$IFDEF MSWINDOWS}Windows, WinSock,{$ENDIF} Should have a ; at the end instead of a ,

Yes, I simply cut away part of the uses clause which was private to my code.

Michael.

Reply by email

Marco van de Voort
to Sven Barth: Bo Berglund via fpc-pascal schrieb am Fr., 10. Sep. 2021, 09:41: There is an API for…

Op 10-9-2021 om 09:46 schreef Sven Barth via fpc-pascal:

You can use the ipconfig command and parse its output (especially ipconfig /all).

Afaik netsh has more info.

I have several tests  for this in Delphi, but they use the iphlp headers that FPC doesn't have yet (a point on my todo list).

I was mainly looking for the core (IP + Mac) and jumbo frame status.

Some info can also be found in the wmi and registry, I attach the wmi related units.

Reply by email

Bo Berglund
to Sven Barth: Bo Berglund via fpc-pascal schrieb am Fr., 10. Sep. 2021, 09:41: There is an API for…
Sven Barth wrote
You can use the ipconfig command and parse its output (especially ipconfig /all).

I also figured that to be the case, so I will run TProcess with Ipconfig /all and then parse the result line by line.

It looks like there is a simple system to it:

Adapter headings start at pos 1 on a line.
Data within the adapter start on pos 4 on each line

And the adapter name starts directly after the word "adapter " on the heading line.

Data for this adapter follow until the next adapter starts.

Should be easy enough to parse out.

Thanks!

Bo Berglund Developer in Sweden

Reply by email

Travis Siegel
to Bo Berglund: Is there a way to enumerate the active adapters on a Windows computer with their IPv4 and…

I don't know how much it will help, but windows has a similar command to linux ifconfig, only on windows it's called ipconfig.

Hope this helps.

Bo Berglund wrote 29 lines
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.

Reply by email

Rainer Stratmann
to Bo Berglund: Is there a way to enumerate the active adapters on a Windows computer with their IPv4 and…

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:

Show quoted text 28 lines
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.

Reply by email