4.2 Changing Your Database Password

To assist you in writing scripts, this section analyzes a sample script that changes the password of your database.

Example 4-3 is useful if you want to reset the database password on a regular basis for security compliance. If you change the database password in a target database, then Enterprise Manager monitoring for that target database is unavailable. To ensure consistent monitoring, you would have to log in to the Enterprise Manager Cloud Control UI, select the required database target and change the password for each and every database, which is very time-consuming.

By using Example 4-3, you can add a number of databases to a group and then change the password for all these databases within the group.

Note:

Line numbers are provided only for explanatory purposes for Table 4-2. For a copy-ready script, see Example A-6 in Sample Scripts.

Example 4-3 dbPasswordChange.py

#Disclaimer
#EXCEPT WHERE EXPRESSLY PROVIDED OTHERWISE, THE SITE, AND ALL CONTENT PROVIDED ON 
#OR THROUGH THE SITE, ARE PROVIDED ON AN "AS IS" AND "AS AVAILABLE" BASIS. ORACLE  
#EXPRESSLY DISCLAIMS ALL WARRANTIES OF ANY KIND, WHETHER EXPRESS OR IMPLIED,  
#INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS 
#FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT WITH RESPECT TO THE SITE AND ALL  
#CONTENT PROVIDED ON OR THROUGH THE SITE. ORACLE MAKES NO WARRANTY THAT: (A) THE  
#SITE OR CONTENT WILL MEET YOUR REQUIREMENTS; (B) THE SITE WILL BE AVAILABLE ON AN  
#UNINTERRUPTED, TIMELY, SECURE,OR ERROR-FREE BASIS; (C) THE RESULTS THAT MAY BE 
#OBTAINED FROM THE USE OF THE SITE OR ANY CONTENT PROVIDED ON OR THROUGH THE SITE 
#WILL BE ACCURATE OR RELIABLE; OR (D) THE QUALITY OF ANY CONTENT PURCHASED OR  
#OBTAINED BY YOU ON OR THROUGH THE SITE WILL MEET YOUR EXPECTATIONS.
#ANY CONTENT ACCESSED, DOWNLOADED OR OTHERWISE OBTAINED ON OR THROUGH THE USE OF 
#THE SITE IS USED AT YOUR OWN DISCRETION AND RISK. ORACLE SHALL HAVE NO  
#RESPONSIBILITY FOR ANY DAMAGE TO YOUR COMPUTER SYSTEM OR LOSS OF DATA THAT 
#RESULTS FROM THE DOWNLOAD OR USE OF CONTENT.
#ORACLE RESERVES THE RIGHT TO MAKE CHANGES OR UPDATES TO, AND MONITOR THE USE OF,  
#THE SITE AND CONTENT PROVIDED ON OR THROUGH THE SITE AT ANY TIME WITHOUT NOTICE.

 1 from emcli  import *
 2 from emcli.exception import VerbExecutionError
 3 import sys
 4 import time
 5 
 6 def check_job_status(job):
 7   count=0
 8   while (count < 10):
 9    count = count + 1
10    obj = emcli.get_jobs(job_id=job)
11    #print obj.out()
12    for entry in obj.out()['data']:
13        l_status = entry['Status ID']
14        l_exec_id = entry['Execution ID']
15        #print entry['Status ID']
16        if (l_status == '5'):
17            print "Job completed successfully"
18            count=100
19        elif (l_status == '4'):
20            l_resp = get_job_execution_detail(execution=l_exec_id, showOutput=True, xml=True)
21            print "Job failed, error details "
22            print "Output " + str(l_resp.out())
23            count=100
24        else:
25            time.sleep(2)
26 
27 def update_db_pwd_for_target(p_target_name, p_target_type, p_old_password, p_new_password):
28    l_target_name = p_target_name
29    l_target_type = p_target_type
30    print "Changing the password for member : name = " + l_target_name + " type = " + l_target_type
31    try :
32        l_resp = update_db_password (target_name=l_target_name,
33                                  target_type = l_target_type,
34                                  change_at_target="yes",
35                                  user_name="dbsnmp",
36                                  old_password=p_old_password,
37                                  new_password=p_new_password,
38                                  retype_new_password=p_new_password)
39        l_job_submitted = l_resp.out()['JobId']
40        check_job_status(l_job_submitted)
41    except emcli.exception.VerbExecutionError, e:
42        print "ERROR : Change Password failed for name = " + l_target_name + " type = " + l_target_type
43        print "ERROR : " + e.error()
44 
45 def update_db_pwd_for_group(p_group, p_old_password, p_new_password):
46    print "Changing the password for group - " + p_group + " from " + p_old_password + " to " + p_new_password
47    members = get_group_members(name=p_group).out()['data']
48    for member in members:
49        l_target_name = member['Target Name']
50        l_target_type = member['Target Type']
51        update_db_pwd_for_target(l_target_name, l_target_type, p_old_password, p_new_password)
52 
53 
54 #Set the OMS URL to connect to
56 set_client_property('EMCLI_OMS_URL','https://myoms.com/em')
57 #Accept all the certificates
58 set_client_property('EMCLI_TRUSTALL','true')
59 
60 login(username=sys.argv[0])
61 
62 
63 l_grp_name = 'maurGroup'
64 
65 l_group_members = ['db1:oracle_database','db2:oracle_database','db3:rac_database']
66 
67 
68 
69 res = create_group(name = l_grp_name, add_targets = l_group_members)
70  
71 print "Listing members for group " + l_grp_name
72 
73 for member in get_group_members(name=l_grp_name).out()['data']:
74    print member
75 
76 
77 y_n_input = raw_input('Now lets change the password for all the members in this group(y/n)')
78 if y_n_input != 'y':
79  exit(0)
80 
81 l_tgt_username = "dbsnmp"
82 l_old_password = "secret1"
83 l_new_password = "secret2"
84   
85 update_db_pwd_for_group(l_grp_name, l_old_password, l_new_password)