0

I occasionally need to customise MATLAB scripts depending on the machine it is being run on. I usually use the following command to grab the computer name:

char(getHostName(java.net.InetAddress.getLocalHost)

This returns a computer name in most cases. However, my laptop (a MacBook) varies depending on which network I am connected to.

Is there a way of retrieving some sort of unique identifier about the computer that does not change depending on the network it is connected to?

Update: I forgot to mention that I am looking for a solution that is OS independent. I need to find a command that works whether on Mac, PC or Linux.

CaptainProg
  • 5,610
  • 23
  • 71
  • 116
  • I am not so knowledgeable in Mac protocols but is the part which change in the name depending on the network not only the suffix (the prefix of the name staying the same) ? – Hoki Mar 17 '16 at 11:50

2 Answers2

0

A good identifier that is independent of network is the MAC Address (nothing to do with macbook). Every computer has a unique MAC Address. You can get it with this command on MATLAB:

system('ifconfig en0 | grep ether')

You will get something like that on the output:

ether 80:e6:50:28:76:d0 
Lincoln Cheng
  • 2,263
  • 1
  • 11
  • 17
0

you can use the Hardware addresses of the computer’s network cards, as suggested here, extracting it with the following Matlab code:

not_win=true;
switch computer('arch')
    case {'maci','maci64'}
        [~,mac_add]=system('ifconfig |grep ether | grep -o -E "([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}"');
    case {'glnx86','glnxa64'}
        [~,mac_add]=system('ifconfig  | grep HWaddr | grep -o -E "([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}"');
    case {'win32','win64'}
        not_win=false;
        sid = '';
        ni = java.net.NetworkInterface.getNetworkInterfaces;
        while ni.hasMoreElements
            addr = ni.nextElement.getHardwareAddress;
            if ~isempty(addr)
                sid = [sid, '.', sprintf('%.2X', typecast(addr, 'uint8'))];
            end
        end
    otherwise, error('Unknown architecture')
end

if(not_win)
    mac_add=regexprep(mac_add,'\r\n|\n|\r','.');
    sid=upper(strrep(mac_add(1:end-1),':',''));
end

The sid variable contains the unique identifier you are looking for. You have to detect the architecture of the machine, because java.net.NetworkInterface.getNetworkInterfaces does not properly work on Unix, returning only running interfaces; so you have to gathered mac addresses parsing the results of ifconfig (take a look here for some examples).

Pay attention! On Mac, if you start a Virtual Machine, fake network interfaces could be added, so the sid may change.

Community
  • 1
  • 1
PieCot
  • 3,564
  • 1
  • 12
  • 20
  • Alas, I've had to un-accept this answer, as it turns out that the `sid` variable changes whenever I move location and connect to a new network – CaptainProg Apr 13 '16 at 18:56
  • This issue concerns the function java.net.NetworkInterface.getNetworkInterfaces, that, on Unix systems, returns only running interfaces. So, if you connect different interfaces to the network, you get a different sid; but, technically, the identitifier is independent from the specific network. – PieCot Apr 13 '16 at 21:21
  • I've updated the answer. Does this version fit your needs? – PieCot Apr 13 '16 at 22:41