Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Apparently, I can find out whether a Samba user needs a password change or not with the following command:

pdbedit -v -u userid | grep "Password must change"

I also see I can list all the Samba users with the command:

pdbedit -L -v

Which outputs about 10MB of data formatted like this:

---------------
Unix username:        egbjt005$
NT username:          egbjt005$
...
Password must change: Thu, 16 Aug 2012 07:33:58 NZST
...
---------------
Unix username:        eghwj001$
NT username:          eghwj001$
...
Password must change: Fri, 20 Jul 2012 14:42:46 NZST
...

The ... indicates several lines of data that's not of interest. The date will be in the past if the password is overdue to be changed.

What I really want is a list of userids starting with a particular 2 character prefix (let's use "eg" as an example) that need a password change. How can I do this?

share|improve this question
    
Can you share the output of those two commands? It will help us build a shell command that will do what you want. –  Jim Paris Aug 13 '12 at 4:46

1 Answer 1

up vote 1 down vote accepted

I'm going on some examples of pdbedit output I found online. Yours might differ; if you can provide some examples of what it prints, this code can be more tailored to what you need.

First, you need to find the usernames that match your prefix. It seems that pdbedit output is in username: foo format, so you could use awk to do that:

pdbedit -L -v | awk '/username:/ {print $2}'

Then we need to find names that start with your prefix, so let's use grep:

pdbedit -L -v | awk '/username:/ {print $2}' | grep ^eg

To see if a particular user's password needs to change, we can just see whether grep found that string, by checking its return code:

if pdbedit -v -u $name | grep -q "Password must change" ; then
    echo $name
fi

Putting it all together with a loop that checks each name:

for name in $(pdbedit -L -v | awk '/username:/ {print $2}' | grep ^eg); do
    if pdbedit -v -u $name | grep -q "Password must change" ; then
        echo $name
    fi
done

Update after you posted sample output

That format is clearly not designed by someone who's a fan of normal Unix command-line text processing tools! While you can, of course, do anything in any language, I'd probably move to something like Python or Perl to parse this one. I chose Perl here because Python's handling of dates and times is abysmal.

#!/usr/bin/perl

use Date::Parse;

$now = time();
while (<>) {
    if (/Unix username:\s*(.*)/) {
        $username = $1;
    }
    if (/Password must change:\s*(.*)/) {
        $change = str2time($1);
        if ($change <= $now) {
            print "$username\n";
        }
    }
}

This will print the Unix username of any user whose password is expired. Use it like:

pdbedit -L -v | perl parse.pl

Edit by Question author

The final code I used was:

#!/usr/bin/perl
use Date::Parse;
$now = time();
while (<>) {
    if (/Unix username:\s*(.*)/) {
        $username = $1;
    }
    if (/Password must change:\s*(.*)/) {
        $expiry = $1;
        $change = str2time($expiry);
        if ($expiry !~ /^never/ && $change <= $now && $username =~ /^eg[a-z]{3}\d+/) {
            print "$username expiry: $expiry\n";
        }
    }
}

Which produces output like this:

egdfd001 expiry: Mon, 30 Jul 2012 08:12:02 NZST
egjpf001 expiry: Mon, 13 Aug 2012 07:50:03 NZST
share|improve this answer
    
Thanks... I've added some example data. It appears there's a date for every record, and it's in the past for users who are overdue for a change. –  Highly Irregular Aug 13 '12 at 5:49
    
Updated answer to account for the output –  Jim Paris Aug 13 '12 at 6:39
    
Brilliant! Thanks very much –  Highly Irregular Aug 15 '12 at 4:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.