LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Solaris / OpenSolaris (https://www.linuxquestions.org/questions/solaris-opensolaris-20/)
-   -   Lastlog script help (https://www.linuxquestions.org/questions/solaris-opensolaris-20/lastlog-script-help-584220/)

yanik 09-12-2007 01:44 PM

Lastlog script help
 
Hi everyone,

I need a little help here. I was told I need to hand over every login from every users of every nix-like systems.

On linux it was quite easy with the lastlog utility. The output looks like this:

Code:

Username        Port    From            Latest
tien                                      **Never logged in**
msimard                                    **Never logged in**
ydoucet          pts/1    10.5.5.2        Wed Sep 12 14:32:24 -0400 2007
nagios                                    **Never logged in**
nbouchard        pts/0    10.9.5.60        Mon Sep 10 14:09:30 -0400 2007
ftpimg                                    **Never logged in**
apache                                    **Never logged in**

But on Solaris I'm having a hard time. I've found lastx, which give me every last login and it looks like this:

Code:

bash-2.05# lastx -u
Username            Logins  Terminal  Host                Last Logged in
etudia10                  3  pts/8      10.9.2.114          Thu Nov 30 16:53:55 2006
etudia08                  3  pts/16    10.9.2.102          Mon Dec 18 14:46:49 2006
scportca                  4  pts/3      10.9.18.55          Wed Sep 12 11:32:24 2007
etudia09                  3  pts/14    10.9.2.104          Thu Nov 30 16:53:29 2006
etudia11                  1  pts/6      10.9.1.71            Thu Nov 30 16:54:17 2006
scolbory                  4  pts/3      10.9.1.60            Wed Jun  7 15:23:51 2006

So far so good, except I don't get those that never logged in. Can someone help me with that? I'm no script guru. I tried something but it doesn't work:

Code:

cat /etc/passwd|cut -d ":" -f1
gives me the login name of all users on the system,
Code:

lastx -u|cut -d " " -f1
gives me the login name of all users that has ever logged in the system.

I tried

Code:

grep -v `lastx -u|cut -d " " -f1` /etc/passwd|cut -d ":" -f1
with no success.

Anyone?

Thank you very much.

muha 09-12-2007 02:49 PM

I think a for loop could do you some good. Because `lastx -u|cut -d " " -f1` outputs a list so you need to take one item per time. Maybe something like:
for i in `cat /etc/passwd|cut -d ":" -f1`; do grep $i ...

Maybe this one:
Code:

for i in `cat /etc/passwd|cut -d ":" -f1`;do if (lastx -u|cut -d " " -f1|grep -q $i);then echo $i yes; else echo $i no; fi;echo bla; done
On my system it looks like:
Code:

for i in `cat /etc/passwd|cut -d ":" -f1`;do if (last|cut -d " " -f1|grep -q $i);then echo $i yes; else echo $i no; fi;echo bla; done
root yes
bla
bin no
bla
daemon no
bla

So the yes is the users that have logged in.
A littlebit cleaner to only output a list of non-logged in users:
Note that we invert the if statement here with !
This gives us a list of users that have not logged in:
Code:

for i in `cat /etc/passwd|cut -d ":" -f1`;do if ! (last|cut -d " " -f1|grep -q $i);then echo $i; fi; done
Your version:
Code:

for i in `cat /etc/passwd|cut -d ":" -f1`;do if ! (lastx -u|cut -d " " -f1|grep -q $i);then echo $i; fi; done

unSpawn 09-12-2007 03:29 PM

Since this pertains Solaris I'll move the thread there.

yanik 09-12-2007 06:22 PM

hey thanks muha, I'll try that first thing in the morning.

ghostdog74 09-12-2007 07:03 PM

@muha, its better to break down the code for OP to understand and not cram everything into one line.
@OP. Just an example, adapt to your needs
Code:

last > outlast
nawk 'BEGIN{FS=":"}
    FNR==NR{user[$1]; next}
    { FS=" "
      if ( $1 in user ) print $0 
    }
' "/etc/passwd" "outlast"


yanik 09-13-2007 07:14 AM

thanks to all,

muha, your last non logged version is what I was looking for, thank you.

Really appreciate.



Yanik

muha 09-13-2007 08:24 AM

@ghostdog74: yanik started :D

@yanik: no problem, cya :)

muha 10-02-2007 02:39 PM

This is a test. Feel free to ignore.


All times are GMT -5. The time now is 03:22 PM.