|
Home > Archive > Unix Programming > November 2005 > Finding currently logged in users
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Finding currently logged in users
|
|
|
| Hi,
I am writing an application which needs to determine all the users that
are currently logged into the system and then send them messages (using
the 'write' command). Please note that I do not want to broadcast the
message to all the users. I want to send custom message to each user.
The issue that I am facing is: how do I identify all the users who are
currently logged in? The problem with using the "who" command is that
on certain systems (e.g. Mac OS X, SuSE, older Solaris) it truncates
the user name to 8 characters. So if there are 2 users called abcdefghi
and abcdefghij logged in, both show as abcdefgh in the output of the
"who" command.
Each user on the system should have a unique userid UID. Can somebody
suggest to me a way to list the UIDs of all the users that are
currently logged into the system. That will solve the problem because
then I can look into the passwd file to determine their usernames and
send them messages using the 'write' command.
If anybody has other ideas I am eager to listen..
Thanks in advance,
RD.
| |
| Pascal Bourguignon 2005-11-01, 6:11 pm |
| "RD" <rid_dau@yahoo.com> writes:
> The issue that I am facing is: how do I identify all the users who are
> currently logged in? The problem with using the "who" command is that
> on certain systems (e.g. Mac OS X, SuSE, older Solaris) it truncates
> the user name to 8 characters. So if there are 2 users called abcdefghi
> and abcdefghij logged in, both show as abcdefgh in the output of the
> "who" command.
>
> Each user on the system should have a unique userid UID. Can somebody
> suggest to me a way to list the UIDs of all the users that are
> currently logged into the system. That will solve the problem because
> then I can look into the passwd file to determine their usernames and
> send them messages using the 'write' command.
>
> If anybody has other ideas I am eager to listen..
ps axl|awk '/UID/{next;}{print $2}'|sort -u
You can also keep only the UID that have processes with a TTY.
In all cases, you might be interested by the TTY too since users
usually have more than one connection at a time.
ps axl\
|awk '
function pos(n,f,k){
for(i=1;i<n;i++){if(k==f[i]){return(i);}}
return(0);}
/UID/{
n=split($0,f);
uid=pos(n,f,"UID");
tty=pos(n,f,"TTY");if(tty==0){tty=pos(n,f,"TT");}
next;}
{
split($0,r);
if((r[tty]=="?")||(r[tty]=="??")){
next;}
else{
print r[uid],r[tty];}}
'\
|sort -u
--
__Pascal Bourguignon__ http://www.informatimago.com/
I need a new toy.
Tail of black dog keeps good time.
Pounce! Good dog! Good dog!
| |
| Gordon Burditt 2005-11-01, 8:52 pm |
| >The issue that I am facing is: how do I identify all the users who are
>currently logged in? The problem with using the "who" command is that
>on certain systems (e.g. Mac OS X, SuSE, older Solaris) it truncates
>the user name to 8 characters. So if there are 2 users called abcdefghi
>and abcdefghij logged in, both show as abcdefgh in the output of the
>"who" command.
>
>Each user on the system should have a unique userid UID. Can somebody
"should" doesn't necessarily mean that there are no UIDs associated
with more than one username. For example, as distributed, UID 0
belongs to both "root" and "toor" on a FreeBSD system. Having two
logins "uucp" and "uucpmgr" for the same UID but different shells
also wasn't uncommon.
It is also not that uncommon to have another login for "shutdown",
also UID 0, with a script that calls the "shutdown" command as its
shell and a special password, known to building maintenance staff,
to do a clean shutdown of the system in case of air conditioning
failure or scheduled electric power interruptions, in spite of
having *NO* other access to the system at all.
>suggest to me a way to list the UIDs of all the users that are
>currently logged into the system. That will solve the problem because
>then I can look into the passwd file to determine their usernames and
>send them messages using the 'write' command.
username -> uid is a one-way map.
uid -> username is NOT necessarily a one-way map, and there are no
places to record UIDs of logged-in users anyway.
username -> logged-in-terminal is not a one-way map either, and
it's common to choose the most-recently-active one.
The utmp file, variously placed at /etc/utmp or /var/run/utmp, is
where the login information is maintained. The file format consists
of an array of structures, each containing login information for
one user.
It is quite possible that someone extended the maximum length of a
username "on the cheap". To do it right requires changing the
maximum length of a username in <utmp.h>, then recompiling all the
programs that use it or maintain it (init, login, who, last, w,
ssh, etc.). Further, all of the previous utmp and wtmp files are
no longer readable (since the size of the structure changed). If
executables that might use <utmp.h> are distributed as binaries,
this is a problem. Doing it "on the cheap" means that the full
username isn't logged anywhere, and if what is recorded isn't unique,
you're out of luck. But it doesn't break (too much) distributing
binaries.
Have you actually TRIED this with the "write" command? write(1)
uses utmp to figure out what terminal to write to. So, if
"lordhighmasteroftheuniverseanddns" is logged in and listed by who
as "lordhigh", then I suspect that "write lordhigh" will actually
work, and "write lordhighmasteroftheuniverseanddns" might or might
not (in the BSD systems, I suspect it will).
The worst problem you are likely to have here occurs when two or
more users with usernames identical in the first 8 characters are
simultaneously logged in (say, "lordhighmasteroftheuniverseanddns"
and "lordhighmasterofthedatabase"). You will probably send the
messages intended for both users to one of them.
Incidentally, I've found that using write(1) to attract attention
is a particularly ineffective way of doing it. Given the number
of screen-oriented applications (editors, especially), your message
can get lost in a screen refresh or come across as unintelligible.
Gordon L. Burditt
| |
| Roger Leigh 2005-11-02, 7:53 am |
| "RD" <rid_dau@yahoo.com> writes:
> The issue that I am facing is: how do I identify all the users who are
> currently logged in?
See getutent(3).
--
Roger Leigh
Printing on GNU/Linux? http://gimp-print.sourceforge.net/
Debian GNU/Linux http://www.debian.org/
GPG Public Key: 0x25BFB848. Please sign and encrypt your mail.
|
|
|
|
|