Get list of all MAC addresses connected to my router and the IP's

opc0de picture opc0de · Dec 14, 2011 · Viewed 7k times · Source

I want to list all the MAC addresses that are connected to my router i know it's possible because i have seen it done.

I think all applications use WinPcap for this purpose is there a way i can interface it with my delphi application?

Answer

norgepaul picture norgepaul · Dec 14, 2011

There are a couple of ways you can do this. The first is to connect to the router via SNMP and read the atTable (1.3.6.1.2.1.3.1). This will give you a list of IP addresses matched to MAC addresses. You can use the SNMP functionality in Synapse to read the table. To connect to a router that's running SNMPv1 or SNMPv2c you will need the correct read community string. For SNMPv3 you will need the correct authentication details.

Another method is to use ARP. To send an ARP request, you can use the iphlpapi dll. Here's some code that should get you started.

unit MyARP

interface

uses
  Windows, Classes, SysUtils, WinSock;

function SendARP(DestIp: DWORD; srcIP: DWORD; pMacAddr: pointer; PhyAddrLen: Pointer): DWORD;stdcall; external 'iphlpapi.dll';
function MySendARP(const IPAddress: String): String;

implementation

function MySendARP(const IPAddress: String): String;
var
  DestIP: ULONG;
  MacAddr: Array [0..5] of Byte;
  MacAddrLen: ULONG;
  SendArpResult: Cardinal;
begin
  DestIP := inet_addr(PAnsiChar(AnsiString(IPAddress)));
  MacAddrLen := Length(MacAddr);
  SendArpResult := SendARP(DestIP, 0, @MacAddr, @MacAddrLen);

  if SendArpResult = NO_ERROR then
    Result := Format('%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X',
                     [MacAddr[0], MacAddr[1], MacAddr[2],
                      MacAddr[3], MacAddr[4], MacAddr[5]])
  else
    Result := '';
end;

end.

This method will only work on your local subnet.