User Logged In The Most - Bash


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting User Logged In The Most - Bash
# 8  
Old 02-24-2011
Code:
last| awk 'NF>6&&!/logged in/ { gsub(/\(|\)/,"") 
                              split($NF,a,"[+:]")
                              b[$1]+=($NF~/+/)?a[1]*3600+a[2]*60+a[3]:a[1]*60+a[2]
                             }
          END{for (i in b) print b[i], i|"sort -rn|head -1" }' 

160949 panman82

# 9  
Old 02-24-2011
work smarter not harder. man ac

Code:
$ whatis ac
ac (1)               - print statistics about users' connect time


Last edited by frank_rizzo; 02-24-2011 at 09:47 PM.. Reason: fix man page tag
# 10  
Old 02-24-2011
ac is not a general command, I can find the command in AIX, but not in Solaris.
# 11  
Old 02-24-2011
Funny I needed to escape the + i.e. ($NF~/\+/) or I got:

Code:
awk: 0602-521 There is a regular expression error.
        ?*+ not preceded by valid expression.

# 12  
Old 02-24-2011
Quote:
Originally Posted by rdcwayx
ac is not a general command, I can find the command in AIX, but not in Solaris.
see relevant process accounting command for Solaris. not sure what that would be off hand. man acct would be a good start.
# 13  
Old 02-25-2011
Quote:
Originally Posted by frank_rizzo
work smarter not harder. man ac

Code:
$ whatis ac
ac (1)               - print statistics about users' connect time

That is a nice command for totals, but it also includes terminal time. I'm just looking for console time. Although, if ac was what I needed, this would be a good awk:
Code:
ac -p | awk '
BEGIN {
    username = "none";
    total = 0;
}
$1 !~ /total/ {
    if ($NF > total) {
        username = $1;
        total = $NF;
    }
}
END {
    print username;
}
'

---------- Post updated at 02:02 PM ---------- Previous update was at 01:07 PM ----------

Quote:
Originally Posted by rdcwayx
Code:
last| awk 'NF>6&&!/logged in/ { gsub(/\(|\)/,"") 
                              split($NF,a,"[+:]")
                              b[$1]+=($NF~/+/)?a[1]*3600+a[2]*60+a[3]:a[1]*60+a[2]
                             }
          END{for (i in b) print b[i], i|"sort -rn|head -1" }' 

160949 panman82

Nice, very close to what I'm looking for. Before you posted yesterday I came up with something similar, using match() instead of split(). With a few modifications to what you came up with, and parts of what I came up with, this is the result:
Code:
last | awk '
$2 ~ /console/ {
    gsub(/\(|\)/, "", $NF);
    split($NF, times, "[+:]");
    if ($NF ~ /\+/) {
        logins[$1] += times[1] * 1440;
        logins[$1] += times[2] * 60;
        logins[$1] += times[3];
    } else {
        logins[$1] += times[1] * 60;
        logins[$1] += times[2];
    }
}
END {
    username = "none";
    longest = 0;
    for (user in logins) {
        if (logins[user] > longest) {
            username = user;
            longest = logins[user];
        }
    }
    print username;
}
'

Any optimizations or even better ways of doing this? First thought is the END statement, doesn't seem to be a good way to sort the array (on Mac OS X) and get the top/bottom result. Also, I think your day's calculation was wrong, 24 hours in a day * 60 minutes in an hour = 1440. No?

Last edited by Panman82; 02-25-2011 at 04:04 PM.. Reason: Added awk for ac.
# 14  
Old 02-26-2011
Yes, should be 1400, here is the fix, you can get the username directly.

Code:
awk '$2 ~ /console/ { gsub(/\(|\)/,""); split($NF,a,"[+:]")
                      b[$1]+=($NF~/+/)?a[1]*1400+a[2]*60+a[3]:a[1]*60+a[2]
                      if (max<b[$1]) {max=b[$1];name=$1}
                    }END{print name}' infile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Logged in windows user name

Hi All, We use putty to connect to a unix box. We want to identify the folks involved in running certain shell scripts on the server. I want to identify the windows user id of those folks who have logged through Putty to run the scripts.As unix-id is shared by more than two folks, tracking... (3 Replies)
Discussion started by: mdkareemuddin
3 Replies

2. Shell Programming and Scripting

Last user logged in

hi! How can I find into: /var/log/messages.4 /var/log/messages.3 /var/log/messages.2 /var/log/messages.1 /var/log/messages The last user do a login? (for example user1) My idea is to search by the pattern "Accepted password for" buy I necessary search into all files first and in the... (2 Replies)
Discussion started by: guif
2 Replies

3. Shell Programming and Scripting

current logged in user

Hey guys I need a script that reads a login name and verifies if that user is currently logged in i have found few commands like "who" and "users" but i wonder how can i verify it that login name is logged in or not? (3 Replies)
Discussion started by: nishrestha
3 Replies

4. Red Hat

How to confirm an user logged in is a remote user?

How do I confirm if a user logged in, is remote or local? In the case if the user is remote, how to be sure what authentication/method is it using, like LDAP, NIS or other? (2 Replies)
Discussion started by: kirtikjr
2 Replies

5. Shell Programming and Scripting

Does running a cron job of a user require the user to be logged in?

Suppose user 'asdf' is not logged into server 'bbbb', but the server is up. User 'asdf' has cron job. Will it be executed? (1 Reply)
Discussion started by: thulasidharan2k
1 Replies

6. Shell Programming and Scripting

How can one know how much time user logged?

Hello, i know who command gives you the time when particular user logged in. And subtracting today's date and time from the one found in who we can get how much time user logged in. But this can get very much clumsy as we can't subtract date directly in unix . Is there any other way or command... (4 Replies)
Discussion started by: salman4u
4 Replies

7. UNIX for Advanced & Expert Users

Send email as a different user than the user logged in

Hi I am using mailx to send email and am wondering if there is a way I can send the email from a different user than the user logged in. something like do-not-reply@xyz.com Thank you. (1 Reply)
Discussion started by: rakeshou
1 Replies

8. UNIX and Linux Applications

user logged on any server

the method to find out the all the user who are the current user of the system? i tried with the who. but with that i receive the TTY like something. (3 Replies)
Discussion started by: NIMISH AGARWAL
3 Replies

9. Shell Programming and Scripting

user logged on?

Hi, What commands do i need to check if a user is logged on to the network? thanks. (1 Reply)
Discussion started by: provo
1 Replies

10. UNIX for Dummies Questions & Answers

Is user logged on??

How can i check to see if a user is logged on to the network? (1 Reply)
Discussion started by: provo
1 Replies
Login or Register to Ask a Question