jython script for list servers

Posted on 2012-09-17
Last Modified: 2012-09-25
i have a  WAS7.0.0.21 ND on a linux machine, i got below  script from the was_root/scriptLibraries/servers/V70/ ,  so when i run this python script i get no output and it wsadmin tool just brings back to prompt, please help me what i need to do in order to get list of servers, why this script is not working, what else i need  to provide in order it to work

      # Set global constants
      import sys
      import java
      import AdminUtilities

      # Setting up Global Variable within this script
      bundleName = ""
      resourceBundle = AdminUtilities.getResourceBundle(bundleName)

      ## Example 1 List available servers with given server type and node ##
      def listServers(serverType="APPLICATION_SERVER", nodeName="abe12", failonerror=AdminUtilities._BLANK_):
          if (failonerror==AdminUtilities._BLANK_):
          msgPrefix = "listServers("+`serverType`+", "+`nodeName`+", "+`failonerror`+"): "

            # List servers
            print "---------------------------------------------------------------"
            print " AdminServerManagement:  List servers"
            print " Optional parameters: "
            print "         Server type:    "+serverType
            print "         Node name:      "+nodeName
            print " Usage: AdminServerManagement.listServers(\""+serverType+"\", \""+nodeName+"\") "
            print " Return: List of the servers in the cell. The list is filtered based on the server type and node name parameters if they are provided."
            print "---------------------------------------------------------------"
            print " "
            print " "
            # Construct optional parameters
            optionalParamList = []

            if (len(serverType) > 0):
                optionalParamList = ['-serverType', serverType]

            if (len(nodeName) > 0):
                # check if node exists
                node = AdminConfig.getid("/Node:" +nodeName+"/")
                if (len(node) == 0):
                   raise AttributeError(AdminUtilities._formatNLS(resourceBundle, "WASL6040E", ["nodeName", nodeName]))
                optionalParamList = optionalParamList + ['-nodeName', nodeName]
            optionalParamList = optionalParamList + ['-nodeName', nodeName]

            # List servers with specified server type
            servers = AdminTask.listServers(optionalParamList)

            # Convert Jython string to list
            servers = AdminUtilities.convertToList(servers)

            # Loop through each server in server list
            newservers = []
            for aServer in servers:
                # Obtain server and node names
                sname = aServer[0:aServer.find("(")]
                nname = aServer[aServer.find("nodes/")+6:aServer.find("servers/")-1]
                # Retrieve the server config id
                sid = AdminConfig.getid("/Node:"+nname+"/Server:"+sname)
                if (newservers.count(sid) <= 0):
                #AdminUtilities.infoNotice("Server: " + sid)
            return newservers
            typ, val, tb = sys.exc_info()
            if (typ==SystemExit):  raise SystemExit,`val`
            if (failonerror != AdminUtilities._TRUE_):
                print "Exception: %s %s " % (sys.exc_type, sys.exc_value)
                val = "%s %s" % (sys.exc_type, sys.exc_value)
                raise "ScriptLibraryException: ", `val`
                return -1
                return, val, tb), failonerror)
Question by:wasman
    LVL 41

    Expert Comment

    Q: What do you mean by the phrase "I got below  script from ..."?
      Did you copy the routine somewhere?
      I'll need to understand this in order to answer the "why didn't it work" question

    - It is important to realize that the AdminServerManagement library is automatically provided to the wsadmin environment (unless, for some reason, you have removed the scriptLibraries directory).

      So, to "use" the routine, you only need call the method, using syntax similar to:
     AdminServerManagement:  List servers
     Optional parameters:
             Server type:
             Node name:
     Usage: AdminServerManagement.listServers("", "")
     Return: List of the servers in the cell. The list is filtered based on the server type and node name parameters if they are pro

    Open in new window

    By the way, the fact that the routine generates that big banner mean that I don't like it very much.

    On the other hand, you don't indicate the kind of information that you want, or the format of how it should be displayed.

    If, on the other hand, you only want to list the names of the application servers in your cell, you can do something simple like:

    wsadmin>for node in AdminConfig.list( 'Node' ).splitlines() :
    wsadmin>    nodeName = AdminConfig.showAttribute( node, 'name' )
    wsadmin>    for server in AdminConfig.list( 'Server', node ).splitlines() :
    wsadmin>        servName = AdminConfig.showAttribute( server, 'name' )
    wsadmin>        print '  Node: %s\nServer: %s\n' % ( nodeName, servName )

    Open in new window


    Author Comment

    What do you mean by the phrase "I got below  script from ..."?
    yes i planning to use the sample scripts that came with WAS installations,  just experimenting with them, (was_root/scriptLibraries/servers/V70/

      Did you copy the routine somewhere?
    what is routine, i didn't copied any thing except the script from the WAS7 sample scripts

    i just want to make this script work for me, if you help me fix this script by telling me what is missing in it, it will be easier for me modify other sample scripts that came with WAS7,

    please feel free to ask more question if you didn't understand my goal.
    LVL 41

    Expert Comment

    Are you using a wsadmin interactive session, or do you specify a script file to be executed (i.e., by providing the "-f" command line option)?

    Did you modify the script to provide this command parameter?


    Where did that come from?

    My has this as line 99:

    def listServers(serverType="", nodeName="", failonerror=AdminUtilities._BLANK_):

    Open in new window


    Author Comment

    i am providing the script with -f option and yes i modified this value nodeNAme="abc12" and also the serverType="APPLICATION_SERVER", initially they are left blank in example script, for me this line is -->       def listServers(serverType="APPLICATION_SERVER", nodeName="abe12", failonerror=AdminUtilities._BLANK_): is on line number 13
    LVL 41

    Expert Comment

    You shouldn't be modifying the routine.

    You should be specifying values when you call the routine, as I showed above...

    Author Comment

    when don't provide anything also i get nothing, basically comes back to prompt

    def listServers(serverType="", nodeName="", failonerror=AdminUtilities._BLANK_):

    $ sudo -u webadmin ./ -f ~/scripts/
    Realm/Cell Name: <default>
    Username: xx31952
    Password:                                                                                                                                                                WASX7209I: Connected to process "dmgr" on node abc12admgr using SOAP connector;  The type of process is: DeploymentManager
    LVL 41

    Expert Comment

    the way you should be calling it is:

    $ sudo -u webadmin ./ -c "AdminServerManagement.listServers()"

    You have to realize though, that what you get back from this routine is an array / list of the configuration IDs of the configured application servers.

    Which say "execute this command" (i.e., the command after the "-c")
    The "-f", as you show, is used to execute a script.  If you want to, you could create a trivial script file to call this routine, and display more useful information from the results.  For example:

    for server in AdminServerManagement.listServers() :
        Type = AdminConfig.showAttribute( server, 'serverType' )
        name = AdminConfig.showAttribute( server, 'name' )
        print 'ServerType:', Type
        print 'ServerName:', name

    Open in new window


    Author Comment

    that trivial script is working that was excellent, but can use the usage given in /opt/app/WebSphere7.0.0.21/AppServer/scriptLibraries/servers/V70/ script which is

     Usage: AdminServerManagement.listServers(\""+serverType+"\", \""+nodeName+"\") "

    so i used it now i get below error, can you please correct me with this

    $ sudo -u webadmin ./ -c " AdminServerManagement.listServers(\""+serverType+"\", \""+nodeName+"\") "
    Realm/Cell Name: <default>
    Username: webadmin
    Password:                                                                                                                                                               WASX7209I: Connected to process "dmgr" on node lseth321admgr using SOAP connector;  The type of process is: DeploymentManager
     AdminServerManagement:  List servers
     Optional parameters:
             Server type:    +serverType+
             Node name:      +nodeName+
     Usage: AdminServerManagement.listServers("+serverType+", "+nodeName+")
     Return: List of the servers in the cell. The list is filtered based on the server type and node name parameters if they are provided.

    Exception: exceptions.AttributeError WASL6040E: The nodeName:+nodeName+ specified argument does not exist.
    WASX7015E: Exception running command: "AdminServerManagement.listServers("+serverType+", "+nodeName+")"; exception information: exception from Jython:
    Traceback (innermost last):
      File "<input>", line 1, in ?
      File "/opt/app/WebSphere7.0.0.21/AppServer/scriptLibraries/servers/V70/", line 159, in listServers
    ScriptLibraryException: : 'exceptions.AttributeError WASL6040E: The nodeName:+nodeName+ specified argument does not exist.'

    LVL 41

    Accepted Solution

    The problem is related to how you are invoking wsadmin, and providing the information to be used.

    If we ignore the sudo stuff, the call to wsadmin looks like this:

    ./ -c " AdminServerManagement.listServers(\""+serverType+"\", \""+nodeName+"\") "

    Where is the operating system supposed to get the value of "serverType" and "nodeName"?

    What you need to do is provide these values in the call.  For example:

    ./ -c "AdminServerManagement.listServers('APPLICATION_SERVER', 'myNode01' )"

    that is, if you want to list the configuration IDs for all of the APPLICATION_SERVER objects configured for the node named "myNode01".

    If  you want to list the configuration IDs for all of the servers, of every type, in your cell, you can use a call something like:

    ./ -c "AdminServerManagement.listServers( '', '' )"

    Note how empty strings had to be specified, AND that since we used double quotes to start and stop the "command" to be executed by wsadmin, that we used an open, close single quoted string for each of the two parameters.

    The first parameter, i.e., serverType should be something like:


    and the second parameter must be either an empty string (meaning all nodes in the cell), or the name of an existing node in your cell.

    Does that help?

    Author Closing Comment

    sure it helped, earlier i was trying without removing  special charactors, thank you HonorGod,
    LVL 41

    Expert Comment

    You are very welcome.

    Thanks for the grade & points.

    Good luck & have a great day.

    If you are going to be doing much WebSphere Application Server administration using Jython, you may want to take a look at this book:

    From IBM Press (publisher):
    From Amazon:

    Author Comment

    yeah i have that book, but didn't had enough time to learn jython.... i rarely get work on jython...on top i am not good at programming .... trying logic and syntaxs are my hard part, but thanks for advice
    LVL 41

    Expert Comment

    You are welcome.  If you have other questions, just ask.  We'll do what we can to help.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Trying to run a Python File 11 48
    Update a File not in all folders 2 35
    change home folder path 4 28
    Copy Files - Python 7 32
    Variable is a place holder or reserved memory locations to store any value. Which means whenever we create a variable, indirectly we are reserving some space in the memory. The interpreter assigns or allocates some space in the memory based on the d…
    The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
    Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
    The viewer will learn how to dynamically set the form action using jQuery.

    755 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

    16 Experts available now in Live!

    Get 1:1 Help Now