Recent

Author Topic: Consultar la MAC de un equipo desde codigo  (Read 2753 times)

crisares

  • Jr. Member
  • **
  • Posts: 63
Consultar la MAC de un equipo desde codigo
« on: February 16, 2020, 05:16:42 am »
Hola a todos! Alguno me podria explicar como obtener la MAC Adress de un equipo desde codigo?
Gracias

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2020
  • Former Delphi 1-7, 10.2 user

Thaddy

  • Hero Member
  • *****
  • Posts: 14213
  • Probably until I exterminate Putin.
Re: Consultar la MAC de un equipo desde codigo
« Reply #2 on: February 16, 2020, 11:45:41 am »
There are several little quibles with that code, because it is rather old.
I've updated it a bit:
Code: Pascal  [Select][+][-]
  1. {$mode delphi}{$H+}
  2. uses classes,process;
  3. var
  4.   AProcess:TProcess;
  5.   AStringList:TStrings;
  6. begin
  7.   AProcess := TProcess.Create(nil);
  8.   AStringList := TStringList.Create;
  9.   try  
  10.     AProcess.Executable :='cat';
  11.     AProcess.Parameters.Add('/sys/class/net/eth0/address');
  12.     AProcess.Options := AProcess.Options + [poWaitOnExit, poUsePipes];
  13.     AProcess.Execute;
  14.     AStringList.LoadFromStream(AProcess.Output);
  15.     writeln(AStringList.Text);
  16.   finally
  17.     AStringlist.Free;
  18.     Aprocess.Free;
  19.   end;
  20. end.
Specialize a type, not a var.

crisares

  • Jr. Member
  • **
  • Posts: 63
Re: Consultar la MAC de un equipo desde codigo
« Reply #3 on: February 18, 2020, 05:17:38 pm »
Thanks for the answers ... I'll see if I achieve anything ...

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Consultar la MAC de un equipo desde codigo
« Reply #4 on: February 18, 2020, 07:00:40 pm »
Note that you should be able to access that info directly (just as cat does), so something like this should work:

Code: Pascal  [Select][+][-]
  1. program showmac;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   FileUtil;
  7.   {LazUtils package needed, but note that building
  8.    your own ReadFileToString is quite easy}
  9.  
  10. var
  11.   MAC: String;
  12.  
  13. begin
  14.   MAC := ReadFileToString('/sys/class/net/eth2/address');
  15.   WriteLn(MAC);
  16. end.

Note that in some cases (as shown in the code, for my computer) there may not be eth0 or it may not be the one connected if there are several network interfaces, which is normal these days when there is usually at least two: an ethernet one and a wireless one.
« Last Edit: February 18, 2020, 07:04:36 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

crisares

  • Jr. Member
  • **
  • Posts: 63
Re: Consultar la MAC de un equipo desde codigo
« Reply #5 on: February 19, 2020, 06:57:19 pm »
Note that you should be able to access that info directly (just as cat does), so something like this should work:

Code: Pascal  [Select][+][-]
  1. program showmac;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   FileUtil;
  7.   {LazUtils package needed, but note that building
  8.    your own ReadFileToString is quite easy}
  9.  
  10. var
  11.   MAC: String;
  12.  
  13. begin
  14.   MAC := ReadFileToString('/sys/class/net/eth2/address');
  15.   WriteLn(MAC);
  16. end.

Note that in some cases (as shown in the code, for my computer) there may not be eth0 or it may not be the one connected if there are several network interfaces, which is normal these days when there is usually at least two: an ethernet one and a wireless one.

Is this example to work on windows? or linux? I need to know to work on windows

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Consultar la MAC de un equipo desde codigo
« Reply #6 on: February 19, 2020, 07:03:17 pm »
Hi!

ReadFileToString will not work on any OS with /sys. It uses the filesize of the file. The filesize in the virtual directories /proc and /sys is zero.

Open a Textfile. Read a string. Close the textfile.
This works.

Winni

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Consultar la MAC de un equipo desde codigo
« Reply #7 on: February 19, 2020, 07:33:06 pm »
ReadFileToString will not work on any OS with /sys. It uses the filesize of the file. The filesize in the virtual directories /proc and /sys is zero.

I tested it in several (4) boxes, each with a different distro (see my signature), and it worked right in all -of them. Can't vouch for any dstro I didn't test, of course: there's always a "jack-in-the-box" waiting for you when using Linux :)
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Consultar la MAC de un equipo desde codigo
« Reply #8 on: February 19, 2020, 08:01:41 pm »
Magical Mystery Tour:

@Lucamar

What kind of magic version of ReadFileToString do you got?

I've got this one:


function ReadFileToString(const Filename: String): String;
var
  SrcHandle: THandle;
  ReadCount: LongInt;
  s: String;
begin
  Result := '';
  s:='';
  try
    Setlength(s, FileSize(Filename));
    if s='' then exit;  // <===== !!!!!!

How did you get your version to work?

Winni

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Consultar la MAC de un equipo desde codigo
« Reply #9 on: February 19, 2020, 08:19:28 pm »
I did nothing special: just wrote, compiled and run that program. And yeah, unsurprisingly that looks like the ReadFileToString in my installation, file:
Code: [Select]
/usr/share/lazarus/2.0.6/components/lazutils/fileutil.inc
Note that the point you're raising applies mostly to "files" which are dinamically (and continuosly) updated; mostly static seudo- and real files are readable as ... well, normal files. At least that has been my experience (and it's a rather long one).
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Consultar la MAC de un equipo desde codigo
« Reply #10 on: February 19, 2020, 08:30:45 pm »
Hi!

I got it. There is a change between /sys and /proc.
In /proc the filesize is nearly always zero.
In /sys the filesize is neary always 4096.

So ReadFileToString works with /sys but not with /proc

So no  mysteries !!

Winni

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Consultar la MAC de un equipo desde codigo
« Reply #11 on: February 19, 2020, 08:46:08 pm »
. At least that has been my experience (and it's a rather long one).

Just btw: Started 1993 with Linux Slackware. Moved 1994 to Suse. On job as Desktop OS and as Server OS since 1995.
Installed my fist SMP machine in Winter 96/97 ......

Winni

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Consultar la MAC de un equipo desde codigo
« Reply #12 on: February 19, 2020, 08:57:40 pm »
. At least that has been my experience (and it's a rather long one).

Just btw: Started 1993 with Linux Slackware. Moved 1994 to Suse. [...]

So like myself, more or less, but with different distros: I started with Slackware too, and moved to Debian (with several forays to RedHat) and so on until now than I'm mostly a 'buntu child :D
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

 

TinyPortal © 2005-2018