Recent

Author Topic: (SOLVED) Need to get username and computername for logging, win and unix  (Read 1062 times)

MarkMLl

  • Hero Member
  • *****
  • Posts: 1219
Re: Need to get username and computername for logging, win and unix
« Reply #15 on: July 11, 2020, 10:24:25 pm »
If the hostname is overriden by some daemon (yellow pages or whatever), then it doesn't reflect that.

Note that is just what I remember from what I read over the years, I don't have real experience with such setups.

I tried to raise that for discussion a couple of months ago. A number of Linux distreaux still try to use YP by default, even if not installed.

Bo, you /do/ know about the "set" command that allows you to see what shell variables are defined, don't you?

MarkMLl
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.

tetrastes

  • Jr. Member
  • **
  • Posts: 60
Re: Need to get username and computername for logging, win and unix
« Reply #16 on: July 11, 2020, 10:37:29 pm »
This function is also in the Unix unit, anyone knows if theres a difference?
There is no GetHostName in linux unit, it is in
Code: Pascal  [Select][+][-]
  1. unit oldlinux deprecated 'Use Baseunix/Unix';

BosseB

  • Sr. Member
  • ****
  • Posts: 257
Re: Need to get username and computername for logging, win and unix
« Reply #17 on: July 11, 2020, 10:44:06 pm »
So... Use the GetHostName function from the linux unit.

http://porthos.ist.utl.pt/docs/fpc/units/node288.html

Does not work either...
I did this:
Code: Pascal  [Select][+][-]
  1. uses
  2.   {$IFNDEF FPC} //Delphi
  3.   Windows,
  4.   {$ENDIF}
  5.   {$IFDEF UNIX}
  6.   Linux,  //Added in order to resolve hostname issue
  7.   {$ENDIF}
  8.   Classes,
  9.   SysUtils;
  10. ....
  11.     {$IFDEF UNIX}
  12.       slHeader.Add('Computer name:   ' + GetHostName; //GetEnvironmentVariable(COMPUTER)); //Using suggested call, not working
  13.     {$ELSE}
  14.       slHeader.Add('Computer name:   ' + GetEnvironmentVariable(COMPUTER));
  15.     {$ENDIF}
But when I try to compile this I get the error:
Code: Text  [Select][+][-]
  1. logagi.pas(285,42) Error: Identifier not found "GetHostName"

But I have tested using the command "hostname" on the command line in a terminal on both Raspbian and Ubuntu and both return a valid reply.
And like Raspbian HOSTNAME is not available as an env variable, but echo $HOSTNAME shows the correct result.
Raspbian and Ubuntu operate the same.

So if I were writing a shellscript I could easily use this command to get the name I want.
But now I am writing a FreePascal console program using Lazarus and then I don't see how I can use this kind of call at all, now that the suggested solution actually does not work...

Seems like I really have to open a file on the disk (cat /etc/hostname) in order to read the hostname from it...

For example making a function like this (tested to be working):

Code: Pascal  [Select][+][-]
  1. {$IFDEF UNIX}
  2. {Since the env var HOSTNAME does not exist on Linux we need to read a file...}
  3. function TLogAGI.MyGetHostName: string;
  4. var
  5.   sl: TStringList;
  6. begin
  7.   Result := '';
  8.   sl := TStringList.Create;
  9.   try
  10.     sl.LoadFromFile('/etc/hostname');
  11.     if sl.Count > 0 then
  12.       Result := sl[0];
  13.   finally
  14.     sl.Free;
  15.   end;
  16. end;
  17. {$ENDIF}

--
Bo Berglund
Sweden

rvk

  • Hero Member
  • *****
  • Posts: 4281
Re: Need to get username and computername for logging, win and unix
« Reply #18 on: July 11, 2020, 10:48:34 pm »
So... Use the GetHostName function from the linux unit.
http://porthos.ist.utl.pt/docs/fpc/units/node288.html
Does not work either...
I did this:
...
But when I try to compile this I get the error:
Code: Text  [Select][+][-]
  1. logagi.pas(285,42) Error: Identifier not found "GetHostName"
What happens if you use baseunix or unix unit like suggested.
Does that contain GetHostName?


BosseB

  • Sr. Member
  • ****
  • Posts: 257
Re: Need to get username and computername for logging, win and unix
« Reply #19 on: July 11, 2020, 10:49:06 pm »
If the hostname is overriden by some daemon (yellow pages or whatever), then it doesn't reflect that.

Note that is just what I remember from what I read over the years, I don't have real experience with such setups.

I tried to raise that for discussion a couple of months ago. A number of Linux distreaux still try to use YP by default, even if not installed.

Bo, you /do/ know about the "set" command that allows you to see what shell variables are defined, don't you?

MarkMLl
I did not know about the set command but it seems like it outputs what the env command does PLUS a whole lot more like something that looks like program code.
env just displays the environment variables and as I stated above that is how I found out that HOSTNAME is not defined as an environment variable.
--
Bo Berglund
Sweden

BosseB

  • Sr. Member
  • ****
  • Posts: 257
Re: Need to get username and computername for logging, win and unix
« Reply #20 on: July 11, 2020, 10:56:00 pm »
So... Use the GetHostName function from the linux unit.
http://porthos.ist.utl.pt/docs/fpc/units/node288.html
Does not work either...
I did this:
...
But when I try to compile this I get the error:
Code: Text  [Select][+][-]
  1. logagi.pas(285,42) Error: Identifier not found "GetHostName"
What happens if you use baseunix or unix unit like suggested.
Does that contain GetHostName?
That is not what was suggested, the uses that was suggested was linux and that failed.
But using unix instead does work so now I do no longer have to read the file /etc/hostname..

Thanks!
--
Bo Berglund
Sweden

winni

  • Hero Member
  • *****
  • Posts: 1775
Re: Need to get username and computername for logging, win and unix
« Reply #21 on: July 11, 2020, 10:56:34 pm »
Hi!

As I told before:
If you got a funny uncivilized Linux then first put the hostname into the environment:
Code: Bash  [Select][+][-]
  1. env HOSTNAME=MySweetRaspi
  2.  
And everything is done.

Winni




BosseB

  • Sr. Member
  • ****
  • Posts: 257
Re: Need to get username and computername for logging, win and unix
« Reply #22 on: July 11, 2020, 11:02:13 pm »
Hi!

As I told before:
If you got a funny uncivilized Linux then first put the hostname into the environment:
Code: Bash  [Select][+][-]
  1. env HOSTNAME=MySweetRaspi
  2.  
And everything is done.

Winni
Well, I do not know on what system my program is running and this is obviously a hack to edit the environment.
Where should that be done?
And it feels like a chicken and egg complication...
--
Bo Berglund
Sweden

rvk

  • Hero Member
  • *****
  • Posts: 4281
And for the username (not tested)

Code: Pascal  [Select][+][-]
  1. uses users,baseunix;
  2. begin
  3.   Writeln(GetUserName(fpgetuid));
  4. end.

Not sure if there is a cross-platform getusername in fpc.
No need for environment reading at all then  8)


Warfley

  • Sr. Member
  • ****
  • Posts: 302
Re: Need to get username and computername for logging, win and unix
« Reply #24 on: July 12, 2020, 12:18:44 am »
Seems like I really have to open a file on the disk (cat /etc/hostname) in order to read the hostname from it...
I don't know about /etc/hostname, but /proc/sys/kernel/hostname is a pseudofile, meaning it a file path that does not correspond to a real file on the disk, but one that the operatingsystem handles specially. Meaning not a single disk operation will be executed when reading/writing to such files. So it is orders of magnitude faster than normal file accesses and should be only be slightly slower than the respected posix function (GetHostname) (probably around a factor of 3 times slower, because you need 3 system calls instead of one, fopen, fread and fclose).

That said, using the posix function of course is preferable if you can do so (in some languages you might not have such easy access like in bash)

winni

  • Hero Member
  • *****
  • Posts: 1775
Re: Need to get username and computername for logging, win and unix
« Reply #25 on: July 12, 2020, 12:45:12 am »
Hi!

As I told before:
If you got a funny uncivilized Linux then first put the hostname into the environment:
Code: Bash  [Select][+][-]
  1. env HOSTNAME=MySweetRaspi
  2.  
And everything is done.

Winni
Well, I do not know on what system my program is running and this is obviously a hack to edit the environment.
Where should that be done?
And it feels like a chicken and egg complication...


Yes - news from the egg :

You have to do that as root -otherwise it won't help you.
System operions have to be done as root.

Winni

PS.:

Posted by: Warfley
I don't know about /etc/hostname ...


Yes, that's true



trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 930
  • Former Delphi 1-7, 10.2 User
For macOS (an actual UNIX-certified OS) and for FreeBSD (and probably all BSD variants) the following is THE way to discover the hostname.

Code: Pascal  [Select][+][-]
  1. function get_Hostname: AnsiString;
  2.  
  3. var
  4.   status : Integer;
  5.   len : size_t;
  6.   p   : PChar;
  7.  
  8. begin
  9.   status := fpSysCtlByName('kern.hostname', Nil, @len, Nil, 0);
  10.   if status <> 0 then RaiseLastOSError;
  11.  
  12.   GetMem(p, len);
  13.  
  14.   try
  15.     status := fpSysCtlByName('kern.hostname', p, @len, Nil, 0);
  16.     if status <> 0 then RaiseLastOSError;
  17.     Result := p;
  18.   finally
  19.     FreeMem(p);
  20.   end;
  21. end;
o Lazarus v2.1.0 r63871, FPC v3.3.1 r46876, macOS 10.14.6 (with sup update), Xcode 11.3.1
o Lazarus v2.1.0 r61574, FPC v3.3.1 r42318, FreeBSD 12.1 amd64 (VMware Fusion VM)
o FPC 3.0.4, FreeBSD 12.2-STABLE r365646 amd64
o Lazarus v2.1.0 r61574, FPC v3.0.4, Ubuntu 18.04 (Parallels VM)

 

TinyPortal © 2005-2018