Re: checking for multiple user in /etc/passwd file. (357 Views)
Reply
Frequent Advisor
tempsample
Posts: 100
Registered: ‎06-20-2013
Message 1 of 5 (374 Views)

checking for multiple user in /etc/passwd file.

Hi,

 

 

I need to check multiple user registered in server.

 

 

I have a basic script like :

 

hostlist=/hometestuser/source
username=/hometestuser/source
HOMEDIR=/hometestuser/source
for HOST in `cat hostlist`
do
echo "====================================================" >> ${HOMEDIR}/logfile.txt
echo "      "
echo "$HOST" >> ${HOMEDIR}/logfile.txt
ssh $HOST grep `cat username` /etc/passwd >> ${HOMEDIR}/logfile.txt
done
echo "====================================================="  >> ${HOMEDIR}/logfile.txt

 

$$$$$$$

 

 

I will be getting the username and hostlist from a file.

 

 

 

When I running for multiple user , I am getting error.

 

>>ssh $HOST grep `cat username` /etc/passwd >> ${HOMEDIR}/logfile.txt

 

 

ssh testhost grep ---> it is reading all username in the file as below.

 

 

ssh testhost grep user1 user2 /etc/passwd

 

 

i want to add -e to grep or need to know any other option is available or not ?

 

 

 

 

Please use plain text.
Honored Contributor
Bill Hassell
Posts: 14,200
Registered: ‎05-29-2000
Message 2 of 5 (361 Views)

Re: checking for multiple user in /etc/passwd file.

[ Edited ]

There are several problems with the concept. First, grep must always be protected from finding the wrong matches. If you have a username of bill, then it will find every line where usernames like bill, billy, waybill, billh, etc are located, but worse, grep doesn't stop at the username -- it will match comments, even a home directory like /home/billing or /home/dashbill

 

So start by testing your grep locally. To match just the username (which always starts in column 1), anchor the search to the beginning of each line:

 

grep ^bill /etc/passwd

 

But that is not enough. grep will still find bill and billh which are not duplicates. So extend the search to the mandatory colon (:) at the end of the name, like this:

 

grep ^bill: /etc/passwd

 

Now for multiple matches, the -e works fine, or you an use egrep with extended regular expressions but I find the multiple bars and extra quotes a bit confusing read when checking scripts.  Here are both ways:

 

grep -e ^bill: -e ^joe: -e ^mary: -e ^jennifer: /etc/passwd

egrep  "^bill:|^joe:|^mary:|^jennifer:" /etc/passwd

 

Now creating a grep pattern list like this is a bit tricky but can be done.

 

So the question is:

 

Are you looking for duplicate user names or duplicate user ID numbers?

For duplicate usernames, you can do something like this:

 

cut -d: -f1 /etc/passwd | sort | uniq -c
or 
cut -d: -f1 /etc/passwd | sort | uniq -c | grep -v " 1 "
(shows only duplicate usernames)

 

 

But to find duplicate user ID's (UID), just run:

 

logins -d

 

 

 

 

Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,877
Registered: ‎03-06-2006
Message 3 of 5 (357 Views)

Re: checking for multiple user in /etc/passwd file.

>hostlist=/hometestuser/source
>username=/hometestuser/source
>HOMEDIR=/hometestuser/source

 

Should this be:

hostlist=${HOMEDIR}/hostlist?

username=${HOMEDIR}/username?

 

>for HOST in `cat hostlist`; do

 

To remove evil cats, change to:

for HOST in $(< $hostlist); do

 

If every output is to the same file, you can redirect the whole for loop:
   echo "===================================================="
   echo "$HOST"
   ssh $HOST grep $(< $username) /etc/passwd
done >> ${HOMEDIR}/logfile.txt

 

Note: The above grep is going to get ALL of the names on one line and that won't work.  You can use -f file but then that file must be on $HOST.  Bill mentioned creating multiple "-e name" patterns on the command line.  But it may be easier to just copy the /etc/passwd file back to this machine to process.

 

   scp $host:/etc/passwd passwd.${HOST}

   grep -f $username passwd.${HOST}

 

But based on Bill's comments, you should have the anchors already in $username:

   ^joe:

  ^sam:

You can of course process the original file and create a temp file with that format:

   sed -e 's/\(.*\)/^\1:/' $username > $username.tmp

 

>For duplicate usernames, you can do something like this:

 

Except for logins(1m), those cut(1) commands would work better on the passwd copied back to the source machine.

Please use plain text.
Frequent Advisor
tempsample
Posts: 100
Registered: ‎06-20-2013
Message 4 of 5 (344 Views)

Re: checking for multiple user in /etc/passwd file.

Hi,

 

Thanks Bill and Dennis for suggestion.

 

from your comment , I can understand that ,in  username file i have to add -e

 

some thing like this

 

 

cat username

 

-e user1

-e user2

 

or I have to use some anchor .

 

but my requirement is , In username file it should be in the below format.

 

cat username

 

user1

user2

 

 

 

 

 

 

Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,877
Registered: ‎03-06-2006
Message 5 of 5 (339 Views)

Re: checking for multiple user in /etc/passwd file.

>In username file it should be in the below format.

 

My above post showed you how to use sed to change the file format.  A slight modification will add -e and anchors:

grep $(sed 's/\(.*\)/-e ^\1:/' $username) passwd.${HOST}

Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation