Aix unix last login need assistance with writing script ksh

Posted on 2012-09-16
Last Modified: 2012-09-20
I need help writing a script to generate list of last login of all users in a server.
user's information is in /etc/passwd file and the command to list the last login of a user is "last username -n 1".
How can i put those two steps together in a script to get the output of last login of all users?
Question by:assistunix
    LVL 68

    Expert Comment

    Did you trY "last" without sspecifying a username?

    LVL 26

    Expert Comment

    Or if you want details for all users, in /etc/passwd file order, and not just those who've logged in try:

    finger -sm root | egrep '^Login';(for user in `cat /etc/passwd | cut -f1,1 -d:  `; do finger -sm $user 2>/dev/null; done) | egrep -v '^Login'

    Open in new window

    LVL 68

    Accepted Solution

    If it has to be a script, so that you can use the "-n 1" flag and also get rid of some garbage:

    awk -F: '{print $1}' /etc/passwd | while read user
       last -n 1 $user 2>/dev/null | egrep -v "wtmp|^$"
    LVL 26

    Assisted Solution

    If you decide to use last opposed to finger then you'll probably want to tweak the syntax slightly; convert the "-n 1" to "-1" to permit the script to also run on Unix (MacOS/Darwin, NetBSD, OpenBSD, FreeBSD, Solaris, AIX...) e.g.

     last -1 $user 2>/dev/null | egrep -v "wtmp|^$"

    Open in new window


    Author Comment

    Thank you both.
    arober11: your finger command syntax doesn't work right, give's an odd output. your last command only provides the listing of the user that i am logged in as, instead of all the users.

    wmp: your command works like magic.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    I have been running these systems for a few years now and I am just very happy with them.   I just wanted to share the manual that I have created for upgrades and other things.  Oooh yes! FreeBSD makes me happy (as a server), no maintenance and I al…
    Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
    Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
    In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now