Question : unlock and unexpire oracle accounts through script

Hi all,

I like to create a small unix shell script to unlock and unexpire all the expired and locked user accounts.

example:

>./myscript.sh

the above command has to unlock and unexpire the expired and locked user accounts

i like a interaction script also,

example :

>./myscript.sh -i

then it has to ask like this for all users who's account is locked and expired.

ABC account is locked.
Do you want to unlock ?  - waiting for user input ( if it is y then it should unlock)

ABC account is expired.
Do you want to unexpire ? - waiting for user input ( if it is y then it should unlock)

Any idea how to implement this.

thanks
kamaraj.s

Answer : unlock and unexpire oracle accounts through script

i am not good in unix stuff as when i compare myself to in oracle..... but came up with the below.. which is working for me. Can you give it a try :

note:
 1) comment out all the debug statements which i had put there for understanding which you think that they are not needed in your actual script...
 2) also the below code connects to sqlplus once to unlock each account which are marked with Y as response which is not 100% right. but it does what you wanted as per your requirement. I think the perfect code should connect to database once to unlock all accounts which have Y in one shot.  I tried for this one but could not get it working.
3) also change this code "scott/tiger@testdb" accordingly with right username/pwd@database in the below code in two places.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
#!/usr/bin/ksh
 
i=0
echo "
set feedback off
set heading off
set pagesize 0
select username
from dba_users
where account_status = 'LOCKED';
exit
" | sqlplus -s scott/tiger@testdb  | \
while read line
do
arr[$i]="$line"
i=$i+1
done
 
echo "locked user list is given below : "
i=0
echo "Total no. of users locked ${#arr[*]} "
while [ $i -lt ${#arr[*]} ]
do
print ${arr[$i]}
(( i=i+1 ))
done
echo "After Array Print"
 
i=0
while [ $i -lt ${#arr[*]} ]
do
  while [ 1 -eq 1 ]
  do
  echo 'Do you want to unlock account ' ${arr[$i]} : '.Enter only Y/N:'
  read arr_flag[$i]
  if [ ${arr_flag[$i]} = 'Y' -o ${arr_flag[$i]} = 'N' ]
  then
     break
  fi
  done
  #echo ${arr_flag[$i]} is the given flag
i=$i+1
done
 
i=0
echo "Below is the content after user response:"
while [ $i -lt ${#arr[*]} ]
do
print ${arr[$i]} : ${arr_flag[$i]}
(( i=i+1 ))
done
 
echo "Now unlocking the locked accounts which are with Y response"
i=0
while [ $i -lt ${#arr[*]} ]
do
 if [ ${arr_flag[$i]} = 'Y' ]
 then
    print executing unlock for  ${arr[$i]} : ${arr_flag[$i]}
    echo "
           set feedback off
           set heading off
           set pagesize 0
           set serveroutput on
begin
execute immediate 'alter user ' || '${arr[$i]}' || ' account unlock ';
exception when others then
  dbms_output.put_line('exception raised :' ||sqlcode );
  dbms_output.put_line('exception raised :' ||sqlerrm );
end;
/
exit
" | sqlplus -s scott/tiger@testdb
 else
    print ${arr[$i]} : ${arr_flag[$i]} - NO UNLOCK required
 fi
(( i=i+1 ))
done
Open in New Window Select All
Random Solutions  
 
programming4us programming4us