LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 01-28-2010, 10:27 AM   #1
logicalfuzz
Member
 
Registered: Aug 2005
Distribution: Arch Linux
Posts: 291

Rep: Reputation: 48
Shell script to find enabled system accounts


I wrote a small script that gets me the list of enabled system accounts in my system.

I figure '*' & '!' (in field #2 of /etc/shadow) mean that the account is disabled or they cannot login, hence 'safe-to-ignore'

Code:
IFS=$'\n'
for i in `cat /etc/passwd` #get each line in the passwd file
        do
        var1=`echo $i | cut -d':' -f3` #get user ID field
        if [ $var1 -lt 500 ] && [ $var1 -gt 0 ] #compare to extract relevant IDs
        then
                grep `echo $i | cut -d':' -f1` /etc/shadow >> /tmp/shadow.out
        fi
        done

grep -v "*" /tmp/shadow.out | cut -d':' -f1
rm -f /tmp/shadow.out
The problem that glares out of the output us that user 'haldaemon' appears twice.
Secondly, i feel this script is a bit of a kludge.. Is there a more elegant way to writing this?

BTW i figure the following command gives me the list of system users, but i am not able to find a way to process it further... :-(

Code:
awk -F":" '$3 >= 1 && $3 < 500 { print $1 }' /etc/passwd
 
Old 01-28-2010, 11:46 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Since that last awk line gives you a list of user names, all you need to do is cycle through the list and grab the shadow line for each one.

I recommend setting up a function for this, then you can run it through sort to put it all in order and remove any duplicates.

Also, unless you have some specific need (i.e. compatibility with shells that don't have it), $() is recommended over `` backticks.

Finally, I think the IFS setting is superfluous here, unless there's a chance that there are spaces or tabs in the lines. And the field separator setting in awk takes care of that anyway.

Code:
#!/bin/bash

function shadowlist() {

     for i in $( awk -F ":" '$3 >= 1 && $3 < 500 { print $1 }' /etc/passwd ) ; do 
               grep "$i" /etc/shadow
     done

}

shadowlist | sort -u     #sort the list output and remove duplicates.

exit 0
By the way, I have no idea why it's printing duplicate lines, but grep seems to think there are multiples for some reason. If you don't want to use "sort -u" to filter them, you can instead use the option "-m 1" in grep to tell it to only print the first match.

Last edited by David the H.; 01-28-2010 at 12:23 PM. Reason: Shortened it up a bit and added note.
 
Old 01-28-2010, 12:59 PM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Ah-ha, I figured out why it's duplicating lines. If you have both "daemon" and "haldaemon" as users, for example, then "grep 'daemon'" matches both users, giving you a duplicate of haldaemon in the output.

Some possible ways I can think of to effectively fix this are to use sort -u on the output, change the grep command to something like 'grep "^$i:"' (the "-m 1" option I gave before wouldn't work in some cases), or to replace grep with something else. You could awk again, for example:
Code:
for i in $( awk -F ":" '$3 >= 1 && $3 < 500 { print $1 }' /etc/passwd ) ; do 
     awk -F ":" -v "user=$i" '$1==user { print }' /etc/shadow
done
No doubt someone will come along and show us a better way, perhaps how to do the whole thing in awk or something.

Last edited by David the H.; 01-28-2010 at 01:07 PM. Reason: additions
 
1 members found this post helpful.
Old 01-29-2010, 08:52 AM   #4
logicalfuzz
Member
 
Registered: Aug 2005
Distribution: Arch Linux
Posts: 291

Original Poster
Rep: Reputation: 48
Quote:
Originally Posted by David the H. View Post
Code:
for i in $( awk -F ":" '$3 >= 1 && $3 < 500 { print $1 }' /etc/passwd ) ; do 
     awk -F ":" -v "user=$i" '$1==user { print }' /etc/shadow
done
Hey thanks David! i took a clue from this piece of code and it works for me :-)
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Create user accounts from shell script? maheshkodamati Linux - Newbie 1 02-26-2008 11:46 AM
Shell script: not able to find jags.singh Programming 8 06-15-2007 10:12 AM
Request : set passwords for many users [user accounts exist] using a shell script bv_uma Linux - Software 3 08-19-2006 09:01 AM
find shell script help liren Linux - Newbie 3 05-02-2005 03:05 PM
how to find the pid of a perl script from shell script toovato Linux - General 1 12-19-2003 06:25 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 11:40 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration