Posted on

Oracle password change script

I always script operations that I do a lot, and with ever shrinking password age settings, scripting a password change is a big time saver. This is the script I use:

#!/bin/bash
# change_password.sh – changes a user’s Oracle password in the current database
#
# written by Dennis Heltzel

usage() {
echo “Usage: $0 “
exit 1
}

if [ $# -lt 1 ]; then
usage
exit 1
fi

PASSWORD_SIZE=20
USER=$1
# 2 options for easily generating a random password
NEWPASS=`openssl rand -base64 ${PASSWORD_SIZE}`
#NEWPASS=`date|md5sum|cut -c-${PASSWORD_SIZE}`

sqlplus -s / as sysdba <<!
alter user $USER identified by "$NEWPASS" account unlock;
prompt Your new password is $NEWPASS
prompt You can change your password anytime with:
prompt alter user $USER identified by "” replace “$NEWPASS”;
prompt Your password will expire in 60 days, please change it before then.

prompt connect $USER/”$NEWPASS”

exit
!

The script assumes your ORACLE_SID is set to the correct database. If you have a number of databases you change passwords in, you could show the current value and allow it to be changed if desired.

I included 2 options for generating a random password, pick either one and see which you prefer.

The script outputs text you can send (securely) to the user. It also outputs the connect string you can use to verify the new password.