Detect if user has a valid MatLab license

53 Ansichten (letzte 30 Tage)
Sean Murphy
Sean Murphy am 19 Jan. 2023
Kommentiert: Jan am 22 Jan. 2023
We're writing a shell script that we want to have it check if the user has a valid MATLAB license. If they do, the script will launch a MATLAB function call from the command line, otherwise it will call an equivalent (but slower) numpy script.
On most of our users' Linux machines, they have their MATLAB license stored under /home/<user>/.matlab/R2021a_licenses. These cases are easy to deal with. However, on some of the machines, the user licenses were installed under /usr/local/MATLAB/R2021a/licenses. On those machines, licenses for multiple users are stored under that directory. For example, we might have a machine where users foo and bar have valid license files, but user baz does not. If I look in /usr/local/MATLAB/R2021a/licenses I might see the following files:
license_MyHost_123456_R2021a.lic, license_MyHost_987654_R2021a.lic
I've looked at the contents of the license file and there doesn't seem to be a human-readable username listed in there, just the license number. So when users foo, bar, and baz each run my bash shell script, is there anything I can key off to determine that users foo and bar do have a license so my script should use the MATLAB path, and baz does not have a MATLAB license, so they get the numpy version? Ideally maybe knowing which of the two license files goes with which user might also be helpful too - identifying that user foo specifically has license 123456, might be helpful too since then I could maybe offer them the option of copying that license file down into /home/foo/.matlab/R2021a_licenses
  6 Kommentare
Sean Murphy
Sean Murphy am 19 Jan. 2023
Ahh, my bad, @Walter Roberson. The responses got collapsed when I read it. I only saw your response and assumed it was a direct response to my question, without seeing @Mario Malic's post for context.
@Mario Malic: To further clarify, what I'm trying to do - I'm trying to determine if a given user even has a valid MATLAB license to begin with before attempting to launch MATLAB. So using MATLAB's "ver" function doesn't help in this case, since the user running our script may not have a MATLAB license at all.
Jan
Jan am 19 Jan. 2023
@Walter Roberson: If you run ver in the forum, the license number 0 is shown. See also:
license
ans = '0'

Melden Sie sich an, um zu kommentieren.

Antworten (3)

Fangjun Jiang
Fangjun Jiang am 19 Jan. 2023
Bearbeitet: Fangjun Jiang am 19 Jan. 2023
There are so many ways to specify the license file, I think you need to follow this order specified in "MATLAB License Search Path" in this link.
Instead, I would suggest launching MATLAB directly. If not successful, then use Python.
  2 Kommentare
Sean Murphy
Sean Murphy am 19 Jan. 2023
Thanks for your response. I assume you're talking about this section in the link you sent me?
--- begin snippet ---
MATLAB License Search Path
MATLAB reads the license file at startup to check for product licensing. MATLAB searches for the license file in the following locations, in the specified order:
  1. The license file specified on MATLAB startup command line using the -c option. The -c option overrides the entire search order and uses only what was specified on the command line. This is the only path MATLAB searches.
  2. Environment variables MLM_LICENSE_FILE and LM_LICENSE_FILE.
  3. The profile folder of the person starting the program
  4. The \licenses folder in the MATLAB installation for files named license.dat or any file with the extension .lic.
After it finds a MathWorks license file, MATLAB stops searching.
--- end snippet ---
If that's the case, then I think I'm Ok.
Cases 1 and 2 don't apply. My bash shell script is the one launching MATLAB (or numpy) and I'm not using the -c option, nor are we setting/using the environment variables.
Case 3 is the easy case I described in my original post as the user having their MATLAB license file in the following folder /home/<user>/.matlab/R2021a_licenses
That only leaves Case 4, which is the one I'm trying to solve, where license files from multiple users are all stored in /usr/local/MATLAB/R2021a/licenses and I somehow need to figure whether any of them belong to the current user
Fangjun Jiang
Fangjun Jiang am 20 Jan. 2023
@Jan has pointed out the expired license or offline with license server.
One more complication, a license file could be license.dat or any *.lic file.

Melden Sie sich an, um zu kommentieren.


Walter Roberson
Walter Roberson am 19 Jan. 2023
Verschoben: Walter Roberson am 19 Jan. 2023
If I look in /usr/local/MATLAB/R2021a/licenses I might see the following files:
license_MyHost_123456_R2021a.lic, license_MyHost_987654_R2021a.lic
In /usr/local/MATLAB/R2021a/licenses you should only see three kinds of licenses:
  • trial licenses can go there
  • Dedicated Host licenses can go there
  • Network licenses (that contain nothing user-specific) can go there
Individual user licenses will not go there.
A license file name format such as license_MyHost_123456_R2021a.lic in that directory would be used for a Dedicated Host license for host name MyHost with MAC address 123456 (if this were Windows then instead of MAC address there could be a disk serial number.) However at the moment I cannot rule out the possibility that they are trial licenses.
Those are not individual user licenses -- not unless your system has been configured with an LM_LICENSE environment variable that points to /usr/local/MATLAB/R2021a/licenses . If so, then in that particular case, the 123456 would be the Mathworks license number. Unfortunately if that is the case, then those files would not contain any information about username. And in such a situation, MATLAB would go through all of the .lic files in directory order, checking to see if someone else is already using that license, and grabbing the first one that is not in use. Which is not how you would want to configure a group of individual Standalone Named User licenses, but that is what would happen if someone did configure that way. (I believe.)
  2 Kommentare
Sean Murphy
Sean Murphy am 19 Jan. 2023
Maybe our license administrator set them up incorrectly, but I can guarantee that the files I'm finding in /usr/local/MATLAB/R2021a/licenses are indeed individual user licenses. Whether they work correctly for those users or not, I don't know since my license file is located under /home/<my_username>/.matlab/R2021a_licenses
For posting here, I did obfuscate the filename, but on the actual machine the files are named with the following format: license_<machine name>_<license number>_R2021a.lic
I have figured out the solution however! In the license files there are string segments that reads like the following:
HOSTID=MATLAB_HOSTID=010203040506:666f6f
where the "010203040506" would be the MAC address of this machine, and the "666f6f" is the username converted from the original ascii format of "foo" to the hexadecimal representation of those character values ('f' = 0x66, 'o' = 0x6f). So now all I have to do is build up a hexadecimal string of the user's username, then grep through each license file to see if any of them contain the user's username hexadecimal string
Walter Roberson
Walter Roberson am 19 Jan. 2023
Oh, interesting!

Melden Sie sich an, um zu kommentieren.


Jan
Jan am 19 Jan. 2023
The detection is not trivial. A matching license file might be there, but the license can be expired. The computer or license server might be offline.
The best way to check, if a user can run Matlab is to call Matlab and check the succeess. If it fails, your program can store a flag in its settings to avoid following checks.
  4 Kommentare
Sean Murphy
Sean Murphy am 20 Jan. 2023
I understand what you're saying, but I disagree. There are a few simple ways (as described above) that I can discover that a given user doesn't have a MATLAB license at all. In those cases, there's no point in ever even attempting to launch MATLAB. Launching MATLAB from the command line takes a couple of seconds and prints out an error message, which just clutters up the terminal.
So as a first step, I think checking for this is appropriate. Then as the next step, if I think they have a license, I can attempt to launch MATLAB and handle the case of MATLAB failing to launch, regardless of whether it's an invalid license problem, or some other issue.
Jan
Jan am 22 Jan. 2023
@Sean Murphy: Yes, of course a check is not inaproppriate. Keep in mind that there can be more ways to run Matlab successfully than you are aware of. Only a successful run of Matlab is a reliable proof, that Matlab runs successfully, which include all kind of versions to perform the license check. Finding a license file is no trustworthy evidence, that the Matlab license is working.
There have been generic license files form Matlab 7, which can be provided from individual locations, which are defined by command line arguments.
Automatic tests are based on assumptions. Maybe letting the user decide actively is more reliable, e.g. with a "-useMatlab" flag in the command line or by a dialog window.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Manage Products finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by