Re: get logged in username

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Thu, Sep 17, 2009 at 11:15:12AM +0430, Mohsen Alimomeni wrote:
> when I do "ssh admin@host", I can get the username "admin", by the
> command "who -m", since there is tty which the username is assigned to
> it.

You also know it's "admin" because you typed "admin" in the ssh command.
Don't be too quick to discount client-side knowledge... though clearly
it's up to you to determine whether the client can be trusted.

> But suppose I want to execute a remote command "ssh admin@host
> myprog", I want to get the username inside the myprog. The command
> "who -m", doesn't work because no tty is created for the user.

You could create a pseudoterminal by running "ssh -t admin@host myprog"
but I suspect this is a red herring.  I think what you're really asking
is "How does a program determine the name of {a,the} user that maps to
the program's {effective,real} UID, apart from running some shell command
like 'whoami'?".

> How can I get the username in this case?

If myprog has access to libc, and host is a Unix-like system, then I
believe the standard approach is:

 1) Call geteuid() to get the effective UID, or getuid() to get the "real"
    UID -- whichever you actually want.

 2) Call getpwuid() to map the UID to a human-readable name.

This really has nothing to do with ssh per se.  It's just standard
Unix/libc programming.

[Index of Archives]     [Open SSH Unix Development]     [Fedora Users]     [Fedora Desktop]     [Yosemite Backpacking]     [KDE Users]     [Gnome Users]

  Powered by Linux