Hi,

Jay SenSharma

Jay SenSharma

Thanks to Magic User “Mr. Baji ”  http://middlewaremagic.com/weblogic/?p=4261#comment-2360 for asking the a very useful query to the magic team….

In Production Environments it is very frequent that for Maintenance/Security reasons we frequently change or Update the DataSource Username and Passwords. But it becomes a problem for WebLogic Administrator when there are multiple DataSources present as part of a WebLogic Domain.

But using the following script it will be very easy for a WebLogic Administrator to change all the DataSource UserName and Passwords Just by Editing a Properties file.  The WLST Script is going to recursively read the Property file and then it will change the DataSource Properties like UserName and Password one by one from Old to the new one.

The Below Mentioned WLST Script can also be enhanced to even change the DataSource URL it means if we want to change the Database to some other failover Database we can do it easily using the following kind of script.

Step1). Create a Directory somewhere in your filesystem like  “C:WLST_MultiDomain_DS”

Step2). Now create a properties file inside “C:WLST_MultiDomain_DS” with some name like “domainsDetails.properties” and write the following kind of entries inside it:

ds.name.1=TestDS_One
ds.username.1=PBPUBLIC
ds.username.new.1=PBPUBLIC_ONE
ds.password.1=PBPUBLIC
ds.password.new.1=PBPUBLIC_ONEPASS

ds.name.2=TestDS_Two
ds.username.2=jack
ds.username.new.2=mack
ds.password.2=password
ds.password.new.2=mackPassword

Step2). Now  write a WLST script inside “C:WLST_MultiDomain_DS” with some name like “Main.py” and write the following kind of entries inside it:

from java.io import FileInputStream

propInputStream = FileInputStream("domainsDetails.properties")
configProps = Properties()
configProps.load(propInputStream)

for i in 1,2:
	mainDSName=configProps.get("ds.name."+ str(i))
	oldDSUserName = configProps.get("ds.username."+ str(i))
	newDSUserName = configProps.get("ds.username.new."+ str(i))
	oldDSPassword = configProps.get("ds.password."+ str(i))
	newDSPassword = configProps.get("ds.password.new."+ str(i))
	i = i + 1

	print("*** Trying to Connect.... *****")
	connect('weblogic','weblogic','t3://localhost:7001')
	print("*** Connected *****")
	cd('/Servers/AdminServer')
	edit()
	startEdit()
	cd('JDBCSystemResources')
	pwd()
	ls()
	allDS=cmo.getJDBCSystemResources()
	for tmpDS in allDS:
		       dsName=tmpDS.getName();
		       print 'DataSource Name: ', dsName
		       print ' '
		       print ' '
		       if  mainDSName == dsName:
		       		print 'Changing Password & UserName for DataSource ', dsName
		       		cd('/JDBCSystemResources/'+dsName+'/JDBCResource/'+dsName+'/JDBCDriverParams/'+dsName)
		       		print('/JDBCSystemResources/'+dsName+'/JDBCResource/'+dsName+'/JDBCDriverParams/'+dsName)
		       		set('PasswordEncrypted', newDSPassword)
		       		cd('/JDBCSystemResources/'+dsName+'/JDBCResource/'+dsName+'/JDBCDriverParams/'+dsName+'/Properties/'+dsName+'/Properties/user')
		       		set('Value',newDSUserName)
		       		print("*** CONGRATES !!! Username & Password has been Changed for DataSource: ", dsName)
		       		print ('')
		       		print ('')

save()
activate()

Step3). Run the “setWLSEnv.sh” by adding two DOTs separated by a single space …..before the actual script like following : (use ‘cd’ command to move inside the <BEA_HOME>/wlserver_10.3/server/bin) then run the following command….
.  ./setWLSEnv.sh
Note: the first DOT represents that set the Environment in the current Shell, AND the second ./ represents execute the script from the current directory.

Step4). Now run the WLS Script like following:

java        weblogic.WLST        Main.py

You wil get the following kind of results:


C:WLST_MultiDomain_DS>java weblogic.WLST Main.py

Initializing WebLogic Scripting Tool (WLST) ...

Welcome to WebLogic Server Administration Scripting Shell

Type help() for help on available commands

*** Trying to Connect.... *****
Connecting to t3://localhost:7001 with userid weblogic ...
Successfully connected to Admin Server 'AdminServer' that belongs to domain '7001_Domain'.

Warning: An insecure protocol was used to connect to the
server. To ensure on-the-wire security, the SSL port or
Admin port should be used instead.

*** Connected *****
Location changed to edit tree. This is a writable tree with
DomainMBean as the root. To make changes you will need to start
an edit session via startEdit().

For more help, use help(edit)

Starting an edit session ...
Started edit session, please be sure to save and activate your
changes once you are done.
drw-   TestDS_One
drw-   TestDS_Two

DataSource Name:  TestDS_One

Changing Password & UserName for DataSource  TestDS_One
/JDBCSystemResources/TestDS_One/JDBCResource/TestDS_One/JDBCDriverParams/TestDS_One
('*** CONGRATES !!! Username & Password has been Changed for DataSource: ', 'TestDS_One')

DataSource Name:  TestDS_Two

*** Trying to Connect.... *****
Connecting to t3://localhost:7001 with userid weblogic ...
Successfully connected to Admin Server 'AdminServer' that belongs to domain '7001_Domain'.

Warning: An insecure protocol was used to connect to the
server. To ensure on-the-wire security, the SSL port or
Admin port should be used instead.

*** Connected *****
You already have an edit session in progress and hence WLST will
continue with your edit session.

Starting an edit session ...
Started edit session, please be sure to save and activate your
changes once you are done.
drw-   TestDS_One
drw-   TestDS_Two

DataSource Name:  TestDS_One

DataSource Name:  TestDS_Two

Changing Password & UserName for DataSource  TestDS_Two
/JDBCSystemResources/TestDS_Two/JDBCResource/TestDS_Two/JDBCDriverParams/TestDS_Two
('*** CONGRATES !!! Username & Password has been Changed for DataSource: ', 'TestDS_Two')

Saving all your changes ...
Saved all your changes successfully.
Activating all your changes, this may take a while ...
The edit lock associated with this edit session is released
once the activation is completed.
Activation completed

C:WLST_MultiDomain_DS>

.

.

Thanks

Jay SenSharma

If you enjoyed this post, please considerleaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.