- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- checking for multiple user in /etc/passwd file.
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-17-2013 11:57 AM
08-17-2013 11:57 AM
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 ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-17-2013 01:23 PM - edited 08-17-2013 01:23 PM
08-17-2013 01:23 PM - edited 08-17-2013 01:23 PM
Re: checking for multiple user in /etc/passwd file.
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
Bill Hassell, sysadmin
- Tags:
- grep
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-17-2013 02:16 PM
08-17-2013 02:16 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-18-2013 10:01 AM
08-18-2013 10:01 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-18-2013 11:33 AM
08-18-2013 11:33 AM
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}