Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Enumerating Users 1

Status
Not open for further replies.

w33mhz

MIS
May 22, 2007
529
US
Does anyone have the basic syntax for a shell script to enumerate local user accounts
 
i am trying to write shell script to change a setting on all my user accounts except for a select few i.e. root
 
cat /etc/passwd | grep "/bin/bash" | cut -f1,4 -d":"

This is a sloppy way of finding accounts with a valid shell (bash) and printing out their name and the 4th field in passwd (whatever that is)...

Conversely, you could remove 'cut' and use a 'sed' substitution ...

Or, you could do
cat /etc/passwd | grep "/bin/bash" | cut -f1 -d":" | xargs usermod .............

D.E.R. Management - IT Project Management Consulting
 
Code:
#!/bin/bash
for i in $(cat /etc/passwd | cut -d ":" -f 1,3)
do
	user=$(echo $i | cut -d ":" -f1)
	uid=$(echo $i | cut -d ":" -f2)
	
	if [[ $uid -gt 499 && $uid -lt 65534 ]]; then
		# Do something with user
		echo $user
	fi
done

--== Anything can go wrong. It's just a matter of how far wrong it will go till people think its right. ==--
 
Excellent, thanks guys I will try that out.
 
Well that seemed to work. Just for reference for anyone here is the script I ran, it changes the password age for all users with a 90 day password.

Code:
#!/bin/bash
for i in $(cat /etc/shadow | cut -d ":" -f 1,5)
do
    user=$(echo $i | cut -d ":" -f1)
    pwdage=$(echo $i | cut -d ":" -f2)
    
    if [[ $pwdage = 90 ]; then
            passwd -x 365 $user
    fi
done

Thanks Zeland for the added logic.
 
OK, whatever does this make you happy:

Code:
#!/bin/bash
for i in $(more /etc/shadow | cut -d ":" -f 1,5)
do
    user=$(echo $i | cut -d ":" -f1)
    pwdage=$(echo $i | cut -d ":" -f2)
    
    if [[ $pwdage = 90 ]; then
            passwd -x 365 $user
    fi
done

There no Useless cat usage
 
My expiry seems to be somewhere else, as I only have 'x' in field 2 of my passwd file. But here is a perl solution that doesn't have to shell out to more/cut/echo all the time. Uncomment the 'system' line when you are sure it's printing the right results...
Code:
#!/usr/bin/perl

use strict;
use warnings;

open(PASSWD, "/etc/passwd") or die "Can't open /etc/passwd $!";

while (<PASSWD>) {
   my ($user, $expiry) = (split ':')[0,1];
   print "passwd -x 365 $user\n") if $expiry == 90;
#  system("passwd -x 365 $user") if $expiry == 90;
}

close(PASSWD);

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top