LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Blogs > camh
User Name
Password

Notices


Rate this Entry

Script to require password change at next login

Posted 12-02-2008 at 05:11 PM by camh

Hi all,

I wrote up this little script to force a password change to a user account on next login. I've found it helpful when doing user administration, since I've found that users generally don't change their passwords unless you force it, and linux doesn't have a 'built-in' script to do so.

Code:
#!/bin/bash

if [ "$(whoami)" != 'root' ]; then
   echo "This program must be run as root"
   exit 1;
 elif [ -z $1 ]; then
    echo "Usage: pwexpire <username>"
    exit 1;
 elif [ ! `grep $1 /etc/passwd` ] ; then
    echo "$1 not found. Please ensure the user account exists."
    exit 1;
 elif [ ! -f /usr/bin/python ] ; then
    echo "Python not found in /usr/bin!"
    exit 1;
fi

echo -e "Please enter the temporary password: \c "
read TMPPASS

if [ ! -f /root/pwconv-tmp.py ]; then
    echo "import crypt; print crypt.crypt(\"$TMPPASS\",\"TK\")" > /root/pwconv-tmp.py

    PWHASH=`/usr/bin/python /root/pwconv-tmp.py`

    usermod -L $1 && chage -d 0 $1
    usermod -p "$PWHASH" $1
    echo "Operation successful. Account $1 will require a password change on next login"
    rm /root/pwconv-tmp.py
    exit 0;
 else echo "An error occured. Please re-run this program"
    exit 1;
fi
Please note that I have hardcoded the salt into the script (denoted by 'TK' in the python command) because my use of this script is for assigning temporary passwords and as such the salt is not that important.

PS: I'm no script wizard, so I'm sure it could be optimized. But, it's functional, which is all that counts

Hope this helps somebody.

-camh
Posted in Uncategorized
Views 1916 Comments 0
« Prev     Main     Next »
Total Comments 0

Comments

 

  



All times are GMT -5. The time now is 04:09 PM.

Main Menu
Advertisement
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