1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
| function macAddress = getMacAddress(delim)
%getMacAddress Get the MAC address of the computer (Windows, Linux and Mac)
%
% macAddress = getMacAddress returns the physical address (MAC) of the
% computer in a character array in the form XX-XX-XX-XX-XX-XX on Windows,
% and in the form xx:xx:xx:xx:xx:xx on Linux and Mac.
%
% macAddress = getMacAddress(delim) uses the single character delim to
% separate the hexadecimal digits.
%
% Examples:
%
% macAddress = getMacAddress()
%
% macAddress =
%
% 12-34-56-78-9A-BC
%
% macAddress = getMacAddress(':')
%
% macAddress =
%
% 12:34:56:78:9A:BC
%
% Author: Jerome Briot
% http://briot-jerome.developpez.com/
% http://www.mathworks.com/matlabcentral/profile/authors/492531-jerome-briot
% Contact: dutmatlab at yahoo dot fr
% Version: 1.0 - 17 Sep 2014
narginchk(0, 1);
if nargin==1 && (~ischar(delim) || ~isscalar(delim))
error('getMacAddress:WrongDelimiter', ...
'Input argument must be a single character (''-'', '':''...)');
end
if ispc
cmd = 'echo off & for /f "delims=," %A in (''getmac /FO csv /NH'') do echo %A';
elseif ismac
cmd = 'ifconfig en0 | grep ether | awk ''{print $NF}''';
elseif isunix
cmd = 'ifconfig eth0 | grep HWaddr | awk ''{print $NF}''';
end
[status, msg] = system(cmd);
if status
error('getMacAddress:NoMacAdress', ...
'Unable to get the MAC address.\n%s', msg);
end
macAddress = strrep(msg, '"', '');
% Now the MAC address should be in one of these two forms:
% xx:xx:xx:xx:xx:xx (Linux or Mac) or XX-XX-XX-XX-XX-XX (Windows)
idx = [strfind(macAddress, ':') strfind(macAddress, '-')];
if numel(idx)~=5 || ~all(idx==[3 6 9 12 15])
error('getMacAddress:NonValidAdress', ...
'Unable to validate the MAC address: %s', macAddress);
end
if nargin==1
macAddress([3 6 9 12 15]) = delim;
end |
Partager