Uncategorized

Weblogic Scripting – list users

Connecting to Weblogic ( WLST )

export PATH=$PATH:$WL_HOME/server/bin
cd $WL_HOME/server/bin
. ./setWLSEnv.sh
java weblogic.WLST

Should then be-able to connect with the following command, type exit() to quit

connect(‘weblogic’,’Admin123′,’localhost:7001′)

To get a list of users from the internal LDAP, first create this properties file <details.properties>

admin.url=t3://localhost:7001
admin.userName=weblogic
admin.password=Admin123

########## User Name which wants to get listed (where, * = all users name) ################
user.name.wildcard=*

########## Maximum Numbers of List you want to see (where, 0 = all the number of users) ###############
maximum.to.return=0

########## If “true” will show all the Authenticator’s Users List, if “false” then would only show Default Authenticators Uers List ################
show.all.authenticator.userlist=false

Create a python script <listusers.py>

#############################################################################

#
# @author Copyright (c) 2010 – 2011 by Middleware Magic, All Rights Reserved.
#
#############################################################################

from java.io import FileInputStream
from weblogic.management.security.authentication import UserReaderMBean

propInputStream = FileInputStream(“details.properties”)
configProps = Properties()
configProps.load(propInputStream)

adminURL=configProps.get(“admin.url”)
adminUserName=configProps.get(“admin.userName”)
adminPassword=configProps.get(“admin.password”)
userNameWildcard=configProps.get(“user.name.wildcard”)
maximumToReturn=configProps.get(“maximum.to.return”)
showAllAuthenticatorUserList=configProps.get(“show.all.authenticator.userlist”)

connect(adminUserName, adminPassword, adminURL)

realmName=cmo.getSecurityConfiguration().getDefaultRealm()
authProvider = realmName.getAuthenticationProviders()

for i in authProvider:
if isinstance(i,UserReaderMBean):
userName = i
authName= i.getName()

if showAllAuthenticatorUserList == ‘true’:
userList = i.listUsers(str(userNameWildcard),int(maximumToReturn))
print ‘======================================================================’
print ‘Below are the List of USERS which are in the: “‘+authName+'”‘
print ‘======================================================================’
num=1
while userName.haveCurrent(userList):
print num,’- ‘+ userName.getCurrentName(userList)
userName.advance(userList)
num=num+1
print ‘======================================================================’
userName.close(userList)

else:
if authName == ‘DefaultAuthenticator’:
userList = i.listUsers(str(userNameWildcard),int(maximumToReturn))
print ‘======================================================================’
print ‘Below are the List of USERS which are in the: “‘+authName+'”‘
print ‘======================================================================’
num=1
while userName.haveCurrent(userList):
print num,’- ‘+ userName.getCurrentName(userList)
userName.advance(userList)
num=num+1
print ‘======================================================================’
userName.close(userList)

Execute via this command, should then list all users in the LDAP tree

java weblogic.WLST userList.py

Review the Weblogic API documentation here

API Docs http://docs.oracle.com/cd/E24329_01/

WLInitialContextFactory  

JNDI context

This Python script allows you to connect to the backend database and retrieve data from a table

#############################################################################
#
# @author Copyright (c) 2010 – 2011 by Middleware Magic, All Rights Reserved.
#
#############################################################################

from com.ziclix.python.sql import zxJDBC

jdbc_url = “jdbc:oracle:thin:@192.168.56.101:1521/pdborcl”
username = “system”
password = “Admin123”
driver = “oracle.jdbc.xa.client.OracleXADataSource”

conn = zxJDBC.connect(jdbc_url, username, password, driver)
cursor = conn.cursor(1)
cursor.execute(“select * from tmp_np”)
cursor.fetchall()
print cursor.rowcount

But this is where you would want to use the LOOKUP function to retrieve connection information from the Weblogic JDBC data source, then connect and pass SQL i.e.

conn = zxJDBC.lookup(jndiName, INITIAL_CONTEXT_FACTORY=factory)

i.e.       weblogic.jndi.WLInitialContextFactory

Where JNDI name is the JNDI name within the Weblogic JDBC data sources.

So this Python Script uses the JDBC/JNDI data source name. So no passwords. Notice the factory is sourced from the weblogic API.  But if the admin server is not running on port 7001, then this will fail, there is a SET method that sets the PROVIDER URL, which we would need to set, otherwise it uses the default URL i.e. localhost 7001…

from com.ziclix.python.sql import zxJDBC

driver = “oracle.jdbc.xa.client.OracleXADataSource”
jndiname = “/jndi/jdbcORCL”
factory = “weblogic.jndi.WLInitialContextFactory”

conn = zxJDBC.lookup(jndiname,INITIAL_CONTEXT_FACTORY=factory )
cursor = conn.cursor(1)
cursor.execute(“select * from tmp_np”)
cursor.fetchall()
print cursor.rowcount

Leave a Reply

Your email address will not be published. Required fields are marked *