[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4546
  • Last Modified:

script to create websphere admin users

i want to write a script to add admin users to websphere. Is it possible to write in jython ? or perl is easier . Any pointers how to go about

Thanks !
0
wasadmin11
Asked:
wasadmin11
  • 11
  • 10
1 Solution
 
HonorGodCommented:
What authentication is being used?

Generally, users are associated with roles (e.g., Administrator).
Is this what you're trying to do?
0
 
wasadmin11Author Commented:
Authentication is federated repositories

A script to add a number of users with roles (admins)
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
HonorGodCommented:
Chapter 12 of this book appears to have what you need:

http://www.ibmpressbooks.com/bookstore/product.asp?isbn=0137009526

Some sample scripts are available from that site:
- click "Downloads"
- Save the "Sample Scripts" zip file
- Look for the script related to chapter 12
0
 
wasadmin11Author Commented:
Thanks for the info. Is it possible to run a jython script inside a shell script
Psuedo code is below: Basically i will read the user names , passwords from a txt file. I dont know how to map the user name to a role (like Administrator)

#!/usr/bin/ksh

NEW_USERS="/path/to/Usernames_file"
 
cat ${NEW_USERS} | \
while read USER PASSWORD
do
`wsadmin -lang jython -c "$AdminTask.createUser {-uid $USER –password PASSWORD -confirmPassword PASSWORD }"`

done
AdminConfig.save()
Sync1 = AdminControl.completeObjectName('type=NodeSync,node=myNodeName,*')
AdminControl.invoke(Sync1, 'sync')
0
 
HonorGodCommented:
> Is it possible to run a jython script inside a shell script

Sure.

Note: The AdminConfig.save() is not necessary.  When you use:

wsadmin.sh -c command

isn't required.

http://publib.boulder.ibm.com/infocenter/wasinfo/v7r0/index.jsp?topic=/com.ibm.websphere.nd.doc/info/ae/ae/rxml_commandline.html

"If you invoke the wsadmin tool with the -c option, any changes that you make to the configuration are saved automatically"

Also, each call to wsadmin.sh would need to start a new JVM, so it would be better to "write" a script with your shell script, and then pass this to wsadmin...

Does this make sense?
0
 
wasadmin11Author Commented:
I have written something like this.

1) How do i map a role like Administrator /Operator to a user/group . Am not seeing any command/method

2) For synching the node . IHow do i get the nodeName ?


Sync1 = AdminControl.completeObjectName('type=NodeSync,node=myNodeName,*')
AdminControl.invoke(Sync1, 'sync')







#!/usr/bin/ksh

# To create WAS users and groups
NEW_USERS="/path/to/users_file"

E_FILE_ACCESS=1
E_ZERO_SIZE=2

if [ ! -r "$1" ]     # Is specified input file readable?

then
  echo "Can't read from input file!"
  echo "Usage: $0 provide user list file name"
  exit $E_FILE_ACCESS


fi                    

if [ -z "$1" ]  # is file zero size ?
then
  echo "Need to specify output file."
  echo "Usage: $0 provide the list of names, groups, password in the file "
  exit $E_WRONG_ARGS
fi

wasadmin.sh -f "createusers.py"


createusers.py




###### To create users  #############

AdminTask.createUser('-uid DCBROWN -password xxxxxxxx -confirmPassword xxxxxxxx -cn DCBROWN -sn DCBROWN')
AdminTask.createUser('-uid DNARMSTRONG -password xxxxxxxx -confirmPassword xxxxxxxx -cn DNARMSTRONG -sn DNARMSTRONG')
AdminTask.createUser('-uid DGADAMS -password xxxxxxx -confirmPassword xxxxxxxx -cn DGADAMS -sn DADAMS')


################To add members to the groups###############################33
AdminTask.addMemberToGroup('-memberUniqueName uid=DCBROWN,o=defaultWIMFileBasedRealm -groupUniqueNamecn=DUSERS,o=defaultWIMFileBasedRealm')
 
AdminTask.addMemberToGroup('-memberUniqueName uid=DNARMSTRONG,o=defaultWIMFileBasedRealm -groupUniqueName cn=DUSERS,o=defaultWIMFileBasedRealm')


AdminTask.addMemberToGroup('-memberUniqueName uid=DNARMSTRONG,o=defaultWIMFileBasedRealm -groupUniqueName cn=DMANAGERS,o=defaultWIMFileBasedRealm')




###### To add bulk users from a file ################

cat ${NEW_USERS} | \
while read USER PASSWORD GROUP
do
AdminTask.createUser('[-uid USER -password PASSWORD -confirmPassword PASSWORD]')
AdminTask.addMemberToGroup('-memberUniqueName uid=USER,o=defaultWIMFileBasedRealm -groupUniqueName cn=USER,o=defaultWIMFileBasedRealm')
done

 

### #####To  create groups##############################
AdminTask.createGroup('-cn users')
AdminTask.createGroup('-cn admins')
################################################

0
 
wasadmin11Author Commented:
i wrote the script in python ( this is my first python script, please bear with me) but am getting ImportError : no module named csv.
Any idea ?

 

import csv
namelist= csv.reader(open('username.csv', 'rb'))

for userid,password,firstname,lastname,groupname,role in namelist:
   
    print '%s, %s, %s, %s, %s, %s'  %(userid, password, firstname, lastname,groupname,role)
    AdminTask.createUser('-uid userid -password password -confirmPassword password -cn firstname -sn lastname')
    AdminTask.addMemberToGroup('-memberUniqueName uid=username,o=defaultWIMFileBasedRealm -groupUniqueNamecn=Admins,o=defaultWIMFileBasedRealm')
    AdminTask.mapUsersToAdminRole('[-roleName role -userids userid]')
    

Open in new window

0
 
wasadmin11Author Commented:
i am using python 27 on windows . In the python shell i can import
>>> import csv
>>> dir (csv)
['Dialect', 'DictReader', 'DictWriter', 'Error', 'QUOTE_ALL', 'QUOTE_MINIMAL', 'QUOTE_NONE', 'QUOTE_NONNUMERIC', 'Sniffer', 'StringIO', '_Dialect', '__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__version__', 'excel', 'excel_tab', 'field_size_limit', 'get_dialect', 'list_dialects', 're', 'reader', 'reduce', 'register_dialect', 'unregister_dialect', 'writer']

but throws error when i run it with wsadmin.bat -f "createusers.py"
0
 
HonorGodCommented:
Ah, remember that wsadmin isn't the same as Python.

wsadmin, when the "-lang jython" parameter is specified, is using a version of Jython, which is a Java implementation of the Python programing language.

It isn't the latest version of Python.  It is, in fact, version 2.1 of Jython (which isn't even the latest and greatest version of Jython :-( ).  And only includes a subset of the available Python modules.

What error is thrown when you execute:


wsadmin -f createusers.py

Open in new window

0
 
HonorGodCommented:
So the error is?

no module named csv.

That's because the libraries that are available to Python aren't the same as the ones available to Jython.

If you execute wsadmin without parameters (or even specify "-conntype none" to get an unconnected, or local mode interactive session), you'll see that csv isn't one of the available libraries/modules
[root@ragdoll bin]# ./wsadmin.sh -conntype none -profileName AppSrv00 -lang jython
WASX7357I: By request, this scripting client is not connected to any server process. Certain configuration and application operations will be available in local mode.
WASX7031I: For help, enter: "print Help.help()"
wsadmin>import csv
WASX7015E: Exception running command: "import csv"; exception information:
 com.ibm.bsf.BSFException: exception from Jython:
Traceback (innermost last):
  File "<input>", line 1, in ?
ImportError: no module named csv

wsadmin>

Open in new window

0
 
wasadmin11Author Commented:
HonorGod,

can you please help me with the code required  ., i am still learning jython  
0
 
HonorGodCommented:
I can try. ;-)

Looking into the csv module to see what it does, and how...
0
 
HonorGodCommented:
Can you provide a sample (CSV) input file for me to use?
0
 
HonorGodCommented:
I tried using a copy of csv.py from a Jython build, but ran into problems. :-(

So, I'm willing to write some code to read / process a CSV into a list of tuples or something.
That might be suitable for you.
0
 
wasadmin11Author Commented:
thanks a lot !. attached is a sample csv file. Basically i want add all the users and map their roles to their ids
username.csv
0
 
HonorGodCommented:
What version of WebSphere are you using?

execute:

/opt/IBM/WebSphere/AppServer/bin/versionInfo.sh

and look for the section entitled "Installed Product"
0
 
wasadmin11Author Commented:
its 6.1.0.27
0
 
wasadmin11Author Commented:
I have been able to write the script :-). I wanted to add the member to the group but am not able to pass the parameters

this is the command line which works, but unable to get it working inside the script

AdminTask.addMemberToGroup('-memberUniqueName uid=DCBROWN,o=defaultWIMFileBasedRealm -groupUniqueName cn=DUSERS,o=defaultWIMFileBasedRealm')

any ideas ?. I want to know in general how to pass parameters/variables to AdminTask or any other wsadmin command.
0
 
wasadmin11Author Commented:
think the script did not get posted ..pasting it below

import sys

filename = "C:\\Documents and Settings\\Administrator\\AST\\workspace\\AdminScriptingProject\\Scripts\\username.txt"
print filename
global AdminConfig
global AdminTask

#filename = sys.argv[ 0 ]
print 'Processing: ' + filename
f = open( filename )
data = f.read()
for lines in data.splitlines():
          print '>>>', lines
          info = lines.split( ',' )
          print info[0],info[1],info[2],info[3],info[4],info[5],info[6]
          AdminTask.createUser( ['-uid',info[0],'-password', info[1],'-confirmPassword', info[1],'-mail',info[2],'-cn',info[3],'-sn',info[4]])
          AdminTask.mapUsersToAdminRole( ['-roleName', info [6], '-userids', info[ 0 ] ] )
f.close()
print 'Finished processing usernames'
AdminConfig.save()
print 'Completed saving the configuration'
 
0
 
HonorGodCommented:
Thanks for the grade & points

Good luck & have a great day.
0
 
crazywolf2010Commented:
Hi,
I am using this script under RHEL WAS 6.x and I have following error.

Can someone please help?

[was61@tmp]$ /opt/IBM/WebSphere/AppServer/bin/wsadmin.sh -conntype none  -lang jython -f  /tmp/create_user.py
WASX7357I: By request, this scripting client is not connected to any server process. Certain configuration and application operations will be available in local mode.
/tmp/username.txt
Processing: /tmp/username.txt
>>> userid,password,email,last name,first name,role
userid password email lastname firstname roleWASX7017E: Exception received while running file "/tmp/create_user.py"; exception information: com.ibm.bsf.BSFException: exception from Jython:
Traceback (innermost last):
  File "<string>", line 15, in ?
IndexError: index out of range: 6

Username.text contains

userid,password,email,last name,first name,role
test,password,test@test.com,tester,tester,administrator

many thanks
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 11
  • 10
Tackle projects and never again get stuck behind a technical roadblock.
Join Now