Добрый день!
Подскажите, пожалуйста, как можно считать MAC-адрес устройства(компьютера) при помощи lazarus на линуксе?
Модератор: Модераторы
aProcess := TProcess.Create(Nil);
{$IFDEF windows}
aProcess.Commandline := 'ipconfig';
{$ENDIF}
{$IFDEF unix}
aProcess.Commandline := 'ifconfig';
{$ENDIF}
aProcess.Options := [poUsePipes,poNoConsole];
aProcess.Execute;
SetLength(aBuffer, 1000);
Repeat
i := aProcess.Output.Read(aBuffer[1], Length(aBuffer));
aString := aString + Copy(aBuffer, 1, i);
Until i = 0;
aProcess.Free;
aStrings := TStringList.Create;
aStrings.Text := aString;
For i := 0 To aStrings.Count - 1 Do
{$IFDEF windows}
If Not(Pos('IP', aStrings[i]) = 0) And Not(Pos(':', aStrings[i]) = 0) Then
Begin
aString := aStrings[i];
Delete(aString, 1, Pos(':', aString));
WriteLn(aString);
End;
{$ENDIF}
{$IFDEF unix}
If Not(Pos('inet', aStrings[i]) = 0) And
Not(Pos('netmask', aStrings[i]) = 0) Then
Begin
aString := aStrings[i];
Delete(aString, 1, Pos('inet', aString) + 4);
aString := Copy(aString, 1, Pos('netmask', aString) - 1);
WriteLn(aString);
End;
{$ENDIF}
aStrings.Free;
// old
{$IFDEF unix}
aProcess.Commandline := 'ifconfig';
{$ENDIF}
//new
{$IFDEF unix}
aProcess.Commandline := '/sbin/ifconfig';
{$ENDIF}
program Project1;
{$mode objfpc}{$H+}
uses
{$IFDEF UNIX}{$IFDEF UseCThreads}
cthreads,
{$ENDIF}{$ENDIF}
Classes, process
{ you can add units after this };
{$R project1.res}
Var
aProcess: TProcess;
aStrings: TStringList;
aString, aBuffer: String;
i: Integer;
begin
aProcess := TProcess.Create(Nil);
{$IFDEF windows}
aProcess.Commandline := 'ipconfig /all';
{$ENDIF}
{$IFDEF unix}
aProcess.Commandline := '/sbin/ifconfig';
{$ENDIF}
aProcess.Options := [poUsePipes,poNoConsole];
aProcess.Execute;
SetLength(aBuffer, 1000);
Repeat
i := aProcess.Output.Read(aBuffer[1], Length(aBuffer));
aString := aString + Copy(aBuffer, 1, i);
Until i = 0;
aProcess.Free;
aStrings := TStringList.Create;
aStrings.Text := aString;
For i := 0 To aStrings.Count - 1 Do
{$IFDEF windows}
If Not(Pos(':', aStrings[i]) = 0) And Not(Pos('-', aStrings[i]) = 0) Then
Begin
aString := aStrings[i];
Delete(aString, 1, Pos(':', aString) + 1);
If Length(aString) = 17 Then WriteLn(aString);
End;
{$ENDIF}
{$IFDEF unix}
If Not(Pos('ether ', aStrings[i]) = 0) Then
Begin
aString := aStrings[i];
Delete(aString, 1, Pos('ether ', aString) + 5);
WriteLn(aString);
End;
{$ENDIF}
aStrings.Free;
ReadLn;
end.
Сейчас этот форум просматривают: нет зарегистрированных пользователей и гости: 24