Deze functie probeert het MAC adres van een netwerk client (desktop, server, laptop, ipad, etc) in jouw network op basis van machine naam of IP adres.
Het MAC Adres is een string in het formaat 00:00:00:00:00:00 en de functie kan worden aangeroepen als
GetMACAddress('machinenaam');
of
GetMacAddres('192.168.1.1');
Als het MAC adres niet gevonden kan worden dan is het antwoord "00:00:00:00:00:00".
Dit werkt overigens alleen onder MacOS X.
Voor Windows of Linux zul je de code moeten aanpassen (post het hier mocht je dat gedaan hebben zodat we een cross-platform functie kunnen bouwen).
Voeg de unti "process" aan de Uses clausule toe.
De "Ping" wordt overigens gebruikt om de zaak even wakker te maken (niet hetzelfde als Wake On Lan!).
function TForm1.Ping(Host:string):boolean;
var s : ansistring;
const PingCount = '1'; // number of pings
PingTimeout = '2'; // timout for each ping
begin
{$IFDEF Darwin}
RunCommand('/sbin/ping',['-c '+PingCount,'-t '+PingTimeout,'-q',Host],s);
{$ELSE}
RunCommand('/bin/ping',['-c '+PingCount,'-w '+PingTimeout,'-q',Host],s);
{$ENDIF}
Result := (pos('100.0% packet loss',S)=0) and (pos('100% packet loss',S)=0);
end;
function TForm1.GetMACAddress(Host:string):string;
var s : ansistring;
MacAddress, MacNo:string;
Counter:integer;
begin
if ping(Host) then
begin
RunCommand('/usr/sbin/arp',[Host],s);
if (pos(':',s)=0) or (pos('(incomplete)',s)>0) or (pos(' -- no entry',s)>0) then
Result := '00:00:00:00:00:00'
else
begin
s := copy(s,pos(' at ',s)+4,Length(s));
s := copy(s,0,pos(' on ',s)-1);
MacAddress:='';
for Counter:=1 to 5 do
begin
MacNo:=Copy(S, 0, pos(':',S)-1);
s:=Copy(s,pos(':',S)+1,Length(S));
if Length(MacNo)=1 then MacNo:='0'+MacNo;
MacAddress:=MacAddress+MacNo+':';
end;
MacAddress:=MacAddress+s;
Result:=UpperCase(MacAddress);
end;
end
else
Result:='00:00:00:00:00:00';
end;