how to separate mac address on windows...?

3 ビュー (過去 30 日間)
Kelvin Viroja
Kelvin Viroja 2016 年 9 月 5 日
回答済み: Xiangrui Li 2017 年 5 月 1 日
system ('getmac'); When i run this command it return 3 mac address... Is it Possible to separate this all 3 mac address or fatch any one of them ?

回答 (1 件)

Xiangrui Li
Xiangrui Li 2017 年 5 月 1 日
For Windows:
[~, a] = system ('getmac');
mac = regexp(a, '([0-9A-F]{2}-){5}[0-9A-F]{2}', 'match', 'once'); % get first one in string
mac = regexp(a, '([0-9A-F]{2}-){5}[0-9A-F]{2}', 'match'); % get all in cellstr
For OSX and Linux, it is a slightly different:
[~, a] = system ('ifconfig');
mac = regexp(a, '([0-9a-f]{2}:){5}[0-9a-f]{2}', 'match', 'once'); % get first one in string
mac = regexp(a, '([0-9a-f]{2}:){5}[0-9a-f]{2}', 'match'); % get all in cellstr
If java is available, it is faster than system command, and the solution is OS-independent:
ni = java.net.NetworkInterface.getNetworkInterfaces;
while ni.hasMoreElements
m = ni.nextElement.getHardwareAddress;
if numel(m)==6 && ~all(m==0) % accept it if length==6 and not all zeros
mac = typecast(m, 'uint8'); % uint8 format
mac = sprintf('%02X-%02X-%02X-%02X-%02X-%02X', mac); % Windows hex format
% mac = sprintf('%02x:%02x:%02x:%02x:%02x:%02x', mac); % unix hex format
break; % stop after finding first one, likely ethernet adaptor
end
end

カテゴリ

Help Center および File ExchangeCall Web Services from MATLAB Using HTTP についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by