Weblogic User Creation/Addition/Listing/Deletion and Group Creation/Deletion Script

Following are the WLST(WebLogic Scripting Tool) commands to add new user/group, to add user in a group, to remove user from a group, to remove user/group, to list all the users.

 

Script(new_user_group.py):
from weblogic.management.security.authentication import UserEditorMBean, UserReaderMBean
print “Connecting to server …”
connect(‘<admin>’,'<password>’,’t3://<hostname>:7001′);
atnr=cmo.getSecurityConfiguration().getDefaultRealm().lookupAuthenticationProvider(“DefaultAuthenticator”)
print “*********User Creation***********”
try:
atnr.createUser(‘1234567′,’oracle123′,’User Description’)
print “User 1234567 created successfully!!”
except Exception:
print “Error: User 1234567 already exists!!”
print “*********Group Creation***********”
group_name = ‘aaa’
try:
print “Creating Group ” + group_name
atnr.createGroup(“” + group_name + “”,”” + group_name + “”)
print “Group ” + group_name + ” created successfully!!”
except Exception:
print “Error creating new Group ” + group_name
print “*********Adding User to New created Group***********”
try:
atnr.addMemberToGroup(“” + group_name + “”,’1234567′)
print “User 1234567 added to ” + group_name + ” Group successfully!!”
except Exception:
print “Error: Failed to add User to ” + group_name + ” Group!!”
print “*********Removing User to New created Group***********”
try:
atnr.removeMemberFromGroup(“” + group_name + “”,’1234567′)
print “User 1234567 removed from group successfully!!”
except Exception:
print “Error: Failed to remove User to ” + group_name + ” Group!!”
print “*********Removing User***********”
try:
atnr.removeUser(‘1234567’)
print “User 1234567 removed successfully!!”
except Exception:
print “Error: Failed to remove User!!”
print “*********Removing Group***********”
try:
atnr.removeGroup(“” + group_name + “”)
print “Group aaa removed successfully!!”
except Exception:
print “Error: Failed to remove Group!!”
print “*********Listing All Users***********”
if isinstance(atnr,UserReaderMBean):
userReader = atnr
cursor = atnr.listUsers(“*”,0)
while userReader.haveCurrent(cursor):
print userReader.getCurrentName(cursor) + “,” + atnr.getUserDescription(userReader.getCurrentName(cursor))
userReader.advance(cursor)
userReader.close(cursor)
Note: Replace the values of <admin>, <password>, and <hostname> with Admin username, Admin Password of Weblogic Admin Server and Content/Capture server Hostname.



Save the file(new_user_group.py)
Give access to the file using the following command:
chmod 777 new_user_group.py
To run the script, use the following command:
/<DB_SERVICE_NAME>/wcapp/Oracle/Middleware/wlserver_10.3/common/bin/wlst.sh new_user_group.py
Output:

*********User Creation***********
User 1234567 created successfully!!
*********Group Creation***********
Creating Group aaa
Group aaa created successfully!!
*********Adding User to New created Group***********
User 1234567 added to aaa Group successfully!!
*********Removing User to New created Group***********
User 1234567 removed from group successfully!!
*********Removing User***********
User 1234567 removed successfully!!
*********Removing Group***********
Group aaa removed successfully!!
If you want to create and add Multiple Users to a group in one script then add the users in a csv(comma separated) file, save the file as user_list.csv,
and run the below script:

from weblogic.management.security.authentication import UserEditorMBean
print “Connecting to server …”
connect(‘<admin>’,'<password>’,’t3://<hostname>:7001′);
f = open(‘user_list.csv’)
for row in f.readlines():
items = row.split(‘,’)
items = [item.strip() for item in items]
uname = items[0]
description = items[1]
for i in items:
if i == description:
continue
if i == uname:
continue
description = description + “, ” + i
name = name.replace(“‘”, “\\'”)
try:
atnr.createUser(“” + uname + “”,”oracle123″,”” + description+ “”)
print “User ” + uname + ” created successfully!!”
except Exception:
print “Error: User ” + uname + ” already exists!!”
try:
atnr.addMemberToGroup(“” + group_name + “”,”” + uname + “”)
print “User ” + uname + ” added to ” + group_name + ” Group successfully!!”
except Exception:
print “Error: Failed to add User to ” + group_name + ” Group!!”
Note: Place both the files in the same folder.

Leave a reply:

Your email address will not be published.

Sliding Sidebar