Link to home
Start Free TrialLog in
Avatar of bross073097
bross073097

asked on

User loged in twice

I'm looking for somthing that will tell me if a user is loged in twice and what port they are loged in on. In the example I would like it to tell me user1 is loged in twice.

I know I can view a who out put but would like to find something to do it for me from a cron or something like that.

My who output looks like this:

user1 ttyC26   Jan 30 11:03
user2 ttyC27   Jan 30 10:51
user3 ttyC15   Jan 30 10:55
user1 ttyC28   Jan 30 11:02
Avatar of Vile
Vile

I just did something like this for an assignment in school. :) I can tell you a method of doing it in C. All login information is saved in a file called utmp. if you load the header file for utmp structure you can read the utmp file and extract the user name. If it appears more than once it will be in there.
if you give me a little bit I can write the program to do it and share the code.
Avatar of bross073097

ASKER

Great!! If you need any other information let me know.

Thanks
who | cut -f1 -d' ' | sort | uniq -c | grep -v '^   1'

will give you a list of users logged in more than once.
That gave me a list of everyone with a 1 infront of them.

Thanks
Bob Rosss
I tested the command on an AIX box, so little changes may be needed.

Proceed step by step and eventually change the command to get it working.

1) who|cut -f1 -d' '
note the space between quotes.
output should be a list of user names

2) who|cut -f1 -d' '|sort
the same list sorted. Multiple instances of the same user name should appear in adjacent lines

3) who|cut -f1 -d' '|sort|uniq -c
each user name compares in the output only once with the number of occurrences

4) who|cut -f1 -d' '|sort|uniq -c|grep -v '^   1'
discard from the previous list users appearing only once. Note the '^' meaning start of line, and count spaces before '1' (in AIX it is three, Linux uniq command may output a different number of leading spaces)

P.S. I checked 'uniq -c' output on a Red Hat 6.0 Linux system: there are six leading blanks. So the last command in the pipe should be grep -v '^      1'

enjoy
I agreed to wait for Vile to write a small program to do what I asked. He still hasn't sent anything yet, but I will give him more time to allow someone else the chance.

Thanks
Bob Ross
Try This:

who | awk '{print $1}' | sort | uniq -c | awk '{print $1 " " $2}' | grep -v ^"1"

The awk helps to format the input.  It will remove the spaces problem.  Run that through awk one more time to only get the usernames and not number of logins.  

This gives just the logins, not the number of times logged in AND the login.

who | awk '{print $1}' | sort | uniq -c | awk '{print $1 " " $2}' | grep -v ^"1"
 | awk '{print $2}'

I can explain it all if you want.
ASKER CERTIFIED SOLUTION
Avatar of hansendc
hansendc

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You don't need three awk invocations and you don't need to count spaces. Change mliberi's solution to this:

who | cut -f1 -d' ' | sort | uniq -c | grep -v '^  *1'

That is a hat, two spaces, a * and a 1 as the regexp for the last grep -v. It selects lines that begin with one or more spaces followed by a 1.

BTW, hansendc's one will not show a username if it happens to have a 1 in it. I guess that's not what you want.

Don't you love the command line?