Solved

script to create websphere admin users

Posted on 2010-08-31
22
3,960 Views
Last Modified: 2013-12-11
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
Comment
Question by:wasadmin11
  • 11
  • 10
22 Comments
 
LVL 41

Expert Comment

by:HonorGod
Comment Utility
What authentication is being used?

Generally, users are associated with roles (e.g., Administrator).
Is this what you're trying to do?
0
 
LVL 41

Expert Comment

by:HonorGod
Comment Utility
0
 
LVL 2

Author Comment

by:wasadmin11
Comment Utility
Authentication is federated repositories

A script to add a number of users with roles (admins)
0
 
LVL 41

Expert Comment

by:HonorGod
Comment Utility
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
 
LVL 2

Author Comment

by:wasadmin11
Comment Utility
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
 
LVL 41

Accepted Solution

by:
HonorGod earned 500 total points
Comment Utility
> 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
 
LVL 2

Author Comment

by:wasadmin11
Comment Utility
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
 
LVL 2

Author Comment

by:wasadmin11
Comment Utility
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
 
LVL 2

Author Comment

by:wasadmin11
Comment Utility
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
 
LVL 41

Expert Comment

by:HonorGod
Comment Utility
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
 
LVL 41

Expert Comment

by:HonorGod
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 2

Author Comment

by:wasadmin11
Comment Utility
HonorGod,

can you please help me with the code required  ., i am still learning jython  
0
 
LVL 41

Expert Comment

by:HonorGod
Comment Utility
I can try. ;-)

Looking into the csv module to see what it does, and how...
0
 
LVL 41

Expert Comment

by:HonorGod
Comment Utility
Can you provide a sample (CSV) input file for me to use?
0
 
LVL 41

Expert Comment

by:HonorGod
Comment Utility
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
 
LVL 2

Author Comment

by:wasadmin11
Comment Utility
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
 
LVL 41

Expert Comment

by:HonorGod
Comment Utility
What version of WebSphere are you using?

execute:

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

and look for the section entitled "Installed Product"
0
 
LVL 2

Author Comment

by:wasadmin11
Comment Utility
its 6.1.0.27
0
 
LVL 2

Author Comment

by:wasadmin11
Comment Utility
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
 
LVL 2

Author Comment

by:wasadmin11
Comment Utility
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
 
LVL 41

Expert Comment

by:HonorGod
Comment Utility
Thanks for the grade & points

Good luck & have a great day.
0
 

Expert Comment

by:crazywolf2010
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now