Recent

Author Topic: Mac Address/Printer Status in Linux  (Read 6882 times)

odvesims

  • Full Member
  • ***
  • Posts: 176
Mac Address/Printer Status in Linux
« on: July 28, 2017, 03:51:14 pm »
Hey There!!

Can anyone please show me how to get Mac Address of my network interface AND the printer status in linux? I use WMI for windows to get this information, but wmi unit is not supported by linux so I'd like a linux-supported equivalent to get such information. Thanks.

odvesims

  • Full Member
  • ***
  • Posts: 176
Re: Mac Address/Printer Status in Linux
« Reply #1 on: July 28, 2017, 05:09:42 pm »
I figured how to read the Mac address, now I'd only need to check the printer status.

Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Re: Mac Address/Printer Status in Linux
« Reply #2 on: July 28, 2017, 05:17:38 pm »
I figured how to read the Mac address

Share it here.

Bart

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1228
Re: Mac Address/Printer Status in Linux
« Reply #3 on: July 28, 2017, 05:21:18 pm »
hello,
for mac address have a look here
for printer status use a runcommand with a  lpq command.

friendly, J.P
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

odvesims

  • Full Member
  • ***
  • Posts: 176
Re: Mac Address/Printer Status in Linux
« Reply #4 on: July 28, 2017, 05:23:45 pm »
I figured how to read the Mac address

Share it here.

Bart

My bad, I meant to share both solutions after I figured the 2nd one. But here it is: (I got it from 'gerson.rodriguez' answer at this thread: http://forum.lazarus.freepascal.org/index.php/topic,10465.msg160800.html#msg160800

Code: Pascal  [Select][+][-]
  1. function getLinuxMacAddress() : string
  2. const
  3.        linux_path = 'sys/class/net/%s/address';
  4.        default_device = 'eth0';
  5. var
  6.        f           : textfield;
  7.        device   : string;
  8.        path      : string;
  9.        address : string;
  10. begin
  11.        Result:= '';
  12.        device = default_device;
  13.        path = Format(linux_path, [device]);
  14.        if not FileExists(path) then
  15.                Result:= '';
  16.        end
  17.        else
  18.        begin
  19.                AssignFile(f, path);
  20.                reset(f);
  21.                readln(f, address);
  22.                Result:= address;
  23.        end;
  24. end;

odvesims

  • Full Member
  • ***
  • Posts: 176
Re: Mac Address/Printer Status in Linux
« Reply #5 on: July 28, 2017, 11:12:15 pm »
hello,
for mac address have a look here
for printer status use a runcommand with a  lpq command.

friendly, J.P

I tried using RunCommand('lpq', myString), but it results in an empty string. I currently do not have any installed/connected printer. Is this why it's empty?

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Mac Address/Printer Status in Linux
« Reply #6 on: July 29, 2017, 11:49:28 am »
I tried using RunCommand('lpq', myString), but it results in an empty string. I currently do not have any installed/connected printer. Is this why it's empty?
Yes, without any printers installed, it will only output error message to stderr, which runcommand doesn't capture (internally it does, but it's not exposed to caller).
« Last Edit: July 29, 2017, 11:54:02 am by Leledumbo »

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1228
Re: Mac Address/Printer Status in Linux
« Reply #7 on: July 29, 2017, 01:39:26 pm »
hello,
to have stderr output on command, you can use a TProcess for example like this (to be improved)  :
Code: Pascal  [Select][+][-]
  1.  var  hprocess: TProcess;
  2.   OutputLines: TStringList;
  3.   OutputError: TstringList;
  4.  begin
  5.  OutputLines:=TStringList.Create;
  6.  OutputError:=TStringList.Create;
  7.  hProcess := TProcess.Create(nil);
  8.  hProcess.Executable := '/usr/bin/lpq';
  9.  hprocess.Parameters.Add('-l');
  10.  hProcess.Options := hProcess.Options + [poWaitOnExit, poUsePipes];
  11.  hProcess.Execute;
  12.  OutputLines.LoadFromStream(hprocess.Output);
  13.  OutputError.Add('Error :');
  14.  OutputError.LoadFromStream(hProcess.Stderr);
  15.  if OutputError.Text <> '' then showMessage(OutputError.Text)
  16.  else     ShowMessage(OutputLines.Text);
  17.  hProcess.Free;
  18.  OutputLines.Free;
  19.  Outputerror.Free;
  20.  end;
if you have CUPS as printing daemon and you want a printer, you can install printer-driver-cups-pdf
(printer driver for PDF writing via CUPS).

Friendly, J.P
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

odvesims

  • Full Member
  • ***
  • Posts: 176
Re: Mac Address/Printer Status in Linux
« Reply #8 on: August 07, 2017, 10:45:22 pm »
I tried using RunCommand('lpq', myString), but it results in an empty string. I currently do not have any installed/connected printer. Is this why it's empty?
Yes, without any printers installed, it will only output error message to stderr, which runcommand doesn't capture (internally it does, but it's not exposed to caller).

I was able to install my printer and I can print to it. However, issuing the "lpq" still returns an empty string. I tried with 'lpstat -p' but that status never changes. I plug/unplug my printer and it keeps saying 'Ready to print'.

odvesims

  • Full Member
  • ***
  • Posts: 176
Re: Mac Address/Printer Status in Linux
« Reply #9 on: August 07, 2017, 10:58:22 pm »
hello,
to have stderr output on command, you can use a TProcess for example like this (to be improved)  :
Code: Pascal  [Select][+][-]
  1.  var  hprocess: TProcess;
  2.   OutputLines: TStringList;
  3.   OutputError: TstringList;
  4.  begin
  5.  OutputLines:=TStringList.Create;
  6.  OutputError:=TStringList.Create;
  7.  hProcess := TProcess.Create(nil);
  8.  hProcess.Executable := '/usr/bin/lpq';
  9.  hprocess.Parameters.Add('-l');
  10.  hProcess.Options := hProcess.Options + [poWaitOnExit, poUsePipes];
  11.  hProcess.Execute;
  12.  OutputLines.LoadFromStream(hprocess.Output);
  13.  OutputError.Add('Error :');
  14.  OutputError.LoadFromStream(hProcess.Stderr);
  15.  if OutputError.Text <> '' then showMessage(OutputError.Text)
  16.  else     ShowMessage(OutputLines.Text);
  17.  hProcess.Free;
  18.  OutputLines.Free;
  19.  Outputerror.Free;
  20.  end;
if you have CUPS as printing daemon and you want a printer, you can install printer-driver-cups-pdf
(printer driver for PDF writing via CUPS).

Friendly, J.P

Says it cannot find executable '/usr/bin/lpq'.

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1228
Re: Mac Address/Printer Status in Linux
« Reply #10 on: August 07, 2017, 11:27:27 pm »
hello,
on my system ( Lubuntu 16.04 64 bits (Debian)  )  lpq is installed with the package cups-bsd . Have you this package or a similar package installed on your system ?
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

odvesims

  • Full Member
  • ***
  • Posts: 176
Re: Mac Address/Printer Status in Linux
« Reply #11 on: August 07, 2017, 11:38:13 pm »
hello,
on my system ( Lubuntu 16.04 64 bits (Debian)  )  lpq is installed with the package cups-bsd . Have you this package or a similar package installed on your system ?

I installed cups-bsd and now it's returning "is Ready, no entries". But this status doesn't change if I plug/unplug the printer.

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1228
Re: Mac Address/Printer Status in Linux
« Reply #12 on: August 08, 2017, 12:59:27 am »
same thing for me with my hp usb printer. With the command lpq Printer is always ready when i unplug it. 
to detect usb printer unplugged try the commands :
Code: Pascal  [Select][+][-]
  1. lpinfo -v
or
Code: Pascal  [Select][+][-]
  1. sudo /usr/lib/cups/backend/usb
« Last Edit: August 08, 2017, 01:01:45 am by Jurassic Pork »
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

odvesims

  • Full Member
  • ***
  • Posts: 176
Re: Mac Address/Printer Status in Linux
« Reply #13 on: August 08, 2017, 03:17:22 pm »
same thing for me with my hp usb printer. With the command lpq Printer is always ready when i unplug it. 
to detect usb printer unplugged try the commands :
Code: Pascal  [Select][+][-]
  1. lpinfo -v
or
Code: Pascal  [Select][+][-]
  1. sudo /usr/lib/cups/backend/usb

Great! lpinfo -v did it for me. How can I speed up the command execution? It's taking +4 seconds. Is there a way I can set a background thread that will check this while the program is executing? I have no experience with multi-threading in lazarus.
« Last Edit: August 08, 2017, 11:34:55 pm by odvesims »

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1228
Re: Mac Address/Printer Status in Linux
« Reply #14 on: August 09, 2017, 12:01:28 am »
How can I speed up the command execution? It's taking +4 seconds. Is there a way I can set a background thread that will check this while the program is executing? I have no experience with multi-threading in lazarus.
For the multithread, have a look to the wiki here
For example you can do something like that :
1 - Create a new unit with the threaded procedure :
Code: Pascal  [Select][+][-]
  1. unit mythread;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6. uses
  7.   Classes, SysUtils;
  8.  
  9. Type
  10.   TShowStatusEvent = procedure(Status: String) of Object;
  11.  
  12.   TMyThread = class(TThread)
  13.   private
  14.     fStatusText : string;
  15.     FOnShowStatus: TShowStatusEvent;
  16.     procedure ShowStatus;
  17.     function ReadInfo() : String;
  18.   protected
  19.     procedure Execute; override;
  20.   public
  21.     Constructor Create(CreateSuspended : boolean);
  22.     property OnShowStatus: TShowStatusEvent read FOnShowStatus write FOnShowStatus;
  23.   end;
  24.  
  25.  
  26.  
  27. implementation
  28. uses process;
  29. constructor TMyThread.Create(CreateSuspended : boolean);
  30. begin
  31.   FreeOnTerminate := True;
  32.   inherited Create(CreateSuspended);
  33. end;
  34.  
  35. procedure TMyThread.ShowStatus;
  36. // this method is executed by the mainthread and can therefore access all GUI elements.
  37. begin
  38.   if Assigned(FOnShowStatus) then
  39.   begin
  40.     FOnShowStatus(fStatusText);
  41.   end;
  42. end;
  43.  
  44. procedure TMyThread.Execute;
  45. var
  46.   newStatus : string;
  47. begin
  48.   fStatusText := 'TMyThread Starting...';
  49.   Synchronize(@Showstatus);
  50.   fStatusText := 'TMyThread Running...';
  51.   NewStatus := ReadInfo();
  52.       if NewStatus <> fStatusText then
  53.         begin
  54.           fStatusText := newStatus;
  55.           Synchronize(@Showstatus);
  56.         end;
  57.  
  58. end;
  59. function TMyThread.ReadInfo(): String;
  60.  var  hprocess: TProcess;
  61.   OutputLines: TStringList;
  62.   OutputError: TstringList;
  63.  begin
  64.  OutputLines:=TStringList.Create;
  65.  OutputError:=TStringList.Create;
  66.  hProcess := TProcess.Create(nil);
  67.  hProcess.Executable := '/usr/sbin/lpinfo';
  68.  hprocess.Parameters.Add('-v');
  69.  hprocess.Parameters.Add('--timeout');
  70.  hprocess.Parameters.Add('1');
  71.  hProcess.Options := hProcess.Options + [poWaitOnExit, poUsePipes];
  72.  hProcess.Execute;
  73.  OutputLines.LoadFromStream(hprocess.Output);
  74.  OutputError.Add('Error :');
  75.  OutputError.LoadFromStream(hProcess.Stderr);
  76.  if OutputError.Text <> '' then ReadInfo := OutputError.Text
  77.  else     ReadInfo := OutputLines.Text;
  78.  hProcess.Free;
  79.  OutputLines.Free;
  80.  Outputerror.Free;
  81.  end;
  82. end.
  83.  
on Unix be sure that you have defined UseCThreads  (custon options : -dUseCThreads ) because you can have this in your lpr file :
Code: Pascal  [Select][+][-]
  1. uses
  2.   {$IFDEF UNIX}{$IFDEF UseCThreads}
  3.   cthreads,
  4.   {$ENDIF}{$ENDIF}  
in your form unit something like that :
Code: Pascal  [Select][+][-]
  1.  
  2. //uses mythread;
  3.  
  4. private
  5.       MyThread: TMyThread;
  6.       procedure ShowStatus(Status: string);
  7. // ================
  8. procedure TForm1.Button6Click(Sender: TObject);
  9. begin
  10.    MyThread := TMyThread.Create(true);
  11.    MyThread.OnShowStatus := @ShowStatus;
  12.    MyThread.Start;
  13. end;    
  14.  
  15. procedure TForm1.FormDestroy(Sender: TObject);
  16. begin
  17.    if MyThread <> Nil then  MyThread.Terminate;
  18. end;
  19.  
  20.  
  21. procedure TForm1.ShowStatus(Status: string);
  22. begin
  23.    Memo1.Append(Status);
  24. end;    
  25.  

Friendly, J.P
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

 

TinyPortal © 2005-2018