Hi, first time poster, newbie to Bash. I'm looking to get the username of the user who's been logged into a computer the most / longest.
I am new to Bash but am familiar with other scripting languages, mainly PHP. So I have a general idea about how to go about the script logic, but don't know all the syntax. So far I can get the list of console users and their session times:
Now I would basically need to:
Loop through each session
Ignore any session time not formatted as (d+hh:mm), ex: current session
Parse the session time and convert to minutes, ex: (2+03:12) = 3072
Add users session time to their logged-in time (array of user => time)
Sort array of users by their logged-in time
Return the top/bottom username (depending on sort order)
I'm going to keep working at this but thought I'd post here in case anyone can help. Thanks for any help you can provide!
Ok, I whipped up what a PHP script would look like. I know there are several ways to do this in PHP but this lists it step-by-step. Also, this would strictly be ran on Mac computers.
PHP Code:
// See updated script below
Last edited by Panman82; 02-17-2011 at 01:24 PM..
Reason: Removed original PHP script, see updated below.
I still need an unprocessed sample of your last output.
As in:
obscure the usernames and Ip addresses and whatever but I need to see what it really looks like. Without that I can't whip up anything I know will work. I can't even tell why you're grepping for 'console' at this point (my 'last' output never contains it).
Sorry, didn't realize that's what you were asking for. Google also was confused when I searched for "bash last". Here is what the output is on our Macs.
---------- Post updated at 11:23 AM ---------- Previous update was at 11:17 AM ----------
Also, I modified the original PHP script above and made it work directly as a script. This does work for me but would like to convert it to Bash if possible... Thanks again for any input! Even if it's "not possible".
PHP Code:
#!/usr/bin/php <?php
// Get raw output from Bash $sessions = shell_exec('last | grep console | awk \'{print $1 " " $9}\'');
// Separate each line into sessions $sessions = explode(PHP_EOL, $sessions);
// Remove last line if empty if (end($sessions) == '') { array_pop($sessions); }
// Separate each session from the user and time array_walk($sessions, create_function('&$e,$k', '$e=explode(" ", $e);'));
// Placeholder for user => logged-in times $login_times = array();
// Loop through each session foreach ($sessions as $session) {
// List the session details list($user, $time) = $session;
// Ignore poorly formatted times // Else parse the time to get d+hh:mm if (!preg_match('/^\(((\d+)\+)?(\d{2})\:(\d{2})\)$/', $time, $matches)) { continue; }
Okay, this works for me when I feed it your last output on stdin:
A pure awk solution might be more elegant but the necessary string operations seem much easier in the shell.
Last edited by Corona688; 02-17-2011 at 02:14 PM..
Thanks for the script. Looking more into awk, I think you are right that it should be able to do everything. I'll do some more digging there. And I realized that the input I posted is actually wrong, using `last | awk '/console/{print $1 " " $NF}'` would just output something like:
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)
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)
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)
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)
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)
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)
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)