Thursday, 27 February 2014

Executing external Python/Jython scripts from within WebSphere Application Server's wsadmin tool

One of my ISSW UK colleagues had asked how to invoke an externally located Python/Jython script from inside the wsadmin tool.

This is different from the normal method of starting wsadmin and directing it to load a file at startup e.g.

$ ./wsadmin.sh -lang jython -c 'AdminApp.list()'

This is subtly different - one is already in wsadmin and wants to go fetch / execute an external script.

Well, thanks to the IBM Connections 2.5 documentation (!), here it is: -

wsadmin>execfile("<$WAS_HOME>/profiles/<DMGR>/config/bin_lc_admin/blogsAdmin.py")

My colleague also pointed out that one can execute OS commands from within a Python shell e.g.

$ python

Python 2.6.6 (r266:84292, May 27 2013, 05:35:12) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.

>>> import os
>>> os.system("/bin/ls")
ConfigBPM.err   ConfigNetworkPort.err  ConfigPWD_USER.err  License.err
ConfigBPM.out   ConfigNetworkPort.out  ConfigPWD_USER.out  License.out
ConfigLocale.err  ConfigNTP.err  ConfigSSH.err      _nowait
ConfigLocale.out  ConfigNTP.out  ConfigSSH.out      ovf-env.ar
ConfigNET.1.err   ConfigPWD_ROOT.err  ConfigVNC.err      ovf-env.done
ConfigNET.1.out   ConfigPWD_ROOT.out  ConfigVNC.out


So, putting it all together, here's me executing an external Jython script from within wsadmin, where said Jython script gets a directory listing: -

$ /opt/IBM/BPM/v85/profiles/BPMPCDmgrNode/bin/wsadmin.sh -lang jython -user wasadmin -password password

WASX7209I: Connected to process "dmgr" on node BPMPCDmgrNode using SOAP connector;  The type of process is: DeploymentManager
WASX7031I: For help, enter: "print Help.help()"

wsadmin>execfile('/tmp/foobar.py')
Hello World!
Here comes the directory listing
Desktop
Documents
Downloads
Music
Pictures
Public
Templates
Videos
workspace


and here's the Python script: -

$ cat /tmp/foobar.py 
import sys;
import os;

print "Hello World!"

print "Here comes the directory listing"

os.system("/bin/ls")

No comments: