Obtenir adresse mac à partir d'une IP

vieuxpere Messages postés 195 Date d'inscription samedi 3 janvier 2004 Statut Membre Dernière intervention 17 décembre 2014 - 16 nov. 2007 à 09:25
cs_cantador Messages postés 4720 Date d'inscription dimanche 26 février 2006 Statut Modérateur Dernière intervention 31 juillet 2021 - 16 nov. 2007 à 18:36
Bonjour,

Je n'arrive pas à trouver un code qui permette d'obtenir l'adresse mac d'un poste distant à partir de son adresse IP en sachant que les deux postes sont sur un meme reseau (et se ping).


Il semble qu'on puisse le faire entre autre par "ARP -a", mais je n'ai trouvé aucun code directement exploitable sur ce sujet, je precise qu'avant de poser la question j'ai ecumez les diffrentes FAQ et forums.

J'ai toutefois trouvé un code qui marche bien pour trouver l'adresse mac du poste local sur lequel j'execute l'application, avez-vous eventuellement une idée pour adapter ce code ci-dessous.

Merci par avance de votre aide précieuse.

Uses : Nb30

function GetAdapterInfo(Lana: Char): String;
var
  Adapter: TAdapterStatus;
  NCB: TNCB;
begin
  FillChar(NCB, SizeOf(NCB), 0);
  NCB.ncb_command := Char(NCBRESET);
  NCB.ncb_lana_num := Lana;
  if Netbios(@NCB) <> Char(NRC_GOODRET) then
  begin
    Result := 'mac not found';
    Exit;
  end;


  FillChar(NCB, SizeOf(NCB), 0);
  NCB.ncb_command := Char(NCBASTAT);
  NCB.ncb_lana_num := Lana;
  NCB.ncb_callname := '*';


  FillChar(Adapter, SizeOf(Adapter), 0);
  NCB.ncb_buffer := @Adapter;
  NCB.ncb_length := SizeOf(Adapter);
  if Netbios(@NCB) <> Char(NRC_GOODRET) then
  begin
    Result := 'mac not found';
    Exit;
  end;
  Result :=
    IntToHex(Byte(Adapter.adapter_address[0]), 2) + '-' +
    IntToHex(Byte(Adapter.adapter_address[1]), 2) + '-' +
    IntToHex(Byte(Adapter.adapter_address[2]), 2) + '-' +
    IntToHex(Byte(Adapter.adapter_address[3]), 2) + '-' +
    IntToHex(Byte(Adapter.adapter_address[4]), 2) + '-' +
    IntToHex(Byte(Adapter.adapter_address[5]), 2);
end;


function GetMACAddress: string;
var
  AdapterList: TLanaEnum;
  NCB: TNCB;
begin
  FillChar(NCB, SizeOf(NCB), 0);
  NCB.ncb_command := Char(NCBENUM);
  NCB.ncb_buffer := @AdapterList;
  NCB.ncb_length := SizeOf(AdapterList);
  Netbios(@NCB);
  if Byte(AdapterList.length) > 0 then
    Result := GetAdapterInfo(AdapterList.lana[0])
  else
    Result := 'mac not found';
end;


Procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage(GetMACAddress);
end;


end.

2 réponses

cs_cantador Messages postés 4720 Date d'inscription dimanche 26 février 2006 Statut Modérateur Dernière intervention 31 juillet 2021 13
16 nov. 2007 à 12:04
unit AddMac;
interface
uses
 Dialogs, SysUtils, nb30; 
function GetAddMac(PCName: string): string;

implementation
function GetAddMac(PCName: string): string;
type
  TAstat = packed record
    adapt: nb30.TAdapterStatus;
    NameBuff: array[0..30] of TNameBuffer;
  end;
var
  Ncb: Tncb;
  Tmp: string;
  pAstat: Pointer;
  Ast: TAstat;
begin
  FillChar(Ncb, SizeOf(Ncb), 0);
  Ncb.ncb_command := Chr(NcbReset);
  NetBios(@Ncb);
  FillChar(Ncb, SizeOf(Ncb), 0);
  FillChar(Ncb.ncb_callname[0], 16, ' ');
  Move(PCName[1], Ncb.ncb_callname[0], Length(PCName));
  Ncb.ncb_command := Chr(NcbAstat);
  Ncb.ncb_lana_num := #0;
  Ncb.ncb_length := SizeOf(AST);
  GetMem(pAstat, Ncb.ncb_length);
  if pAstat = nil then
  begin
    result := 'L''allocation de mémoire a échoué!';
    exit;
  end;
  Ncb.ncb_buffer := pAstat;
  NetBios(@Ncb);
  Move(Ncb.ncb_buffer, Ast, SizeOf(Ast));
  with Ast.adapt do
    Tmp := Format('%.2x-%.2x-%.2x-%.2x-%.2x-%.2x',
      [ord(adapter_address[0]), ord(adapter_address[1]), ord(adapter_address[2]),
      ord(adapter_address[3]), ord(adapter_address[4]), ord(adapter_address[5])]);
  FreeMem(pAstat);
  Result := Tmp;
end;
begin
  ShowMessage(GetAddMac('*'));
end.

çà marche "apparemment" avec le nom du micro
(j'ai fait un ou deux tests mais il n'y a rien de sûr..)
Je te laisse faire le lien entre le nom et l'adresse IP...

cantador
0
cs_cantador Messages postés 4720 Date d'inscription dimanche 26 février 2006 Statut Modérateur Dernière intervention 31 juillet 2021 13
16 nov. 2007 à 18:36
Suis le conseil de Francky23012301 pour les questions et cherche
un peu par toi-même..
On ne peut pas tout faire, sinon on va demander des royalties à Nix..

cantador
0
Rejoignez-nous