#------------------------------------------------------------------------------- # Name: ListPorts() # From: WebSphere Application Server Administration using Jython # Role: Display the Port Numbers configured for each AppServer # History: # date ver who what # -------- --- --- ---------------------------------------------------- # 10/11/01 0.3 rag Fix - configIdAsDict() # 10/10/30 0.2 rag Include showAsDict() so it doesn't need to be imported. # 09/01/08 0.1 rag Minor cleanup for book # 04/30/08 0.0 rag New - written for presentation at IMPACT 2008 #------------------------------------------------------------------------------- import re; import sys; try : if 'AdminConfig' not in dir() : import AdminConfig; except : print 'WebSphere Application Server scripting object unavailable: AdminConfig'; sys.exit() #------------------------------------------------------------------------------- # Name: ListPorts() # Role: Display all of the configured ports by named EndPoint for each server #------------------------------------------------------------------------------- def ListPorts() : SEs = AdminConfig.list( 'ServerEntry' ).splitlines(); #----------------------------------------------------------------------------- # for each ServerEntry configuration ID #----------------------------------------------------------------------------- for SE in SEs : seDict = configIdAsDict( SE ); SEname = seDict[ 'Name' ]; SEnode = seDict[ 'nodes' ]; print ''' Server name: %s Node name: %s\n Port#|EndPoint Name -----+-------------''' % ( SEname, SEnode ); #--------------------------------------------------------------------------- # For the given server (SE) get the list of NamedEndPoints # Then, for each NamedEndPoint, display the port # and endPointName values #--------------------------------------------------------------------------- for NEP in AdminConfig.list( 'NamedEndPoint', SE ).splitlines() : NEPdict = showAsDict( NEP ) EPdict = showAsDict( NEPdict[ 'endPoint' ] ) print '%5d|%s' % ( EPdict[ 'port' ], NEPdict[ 'endPointName' ] ) #------------------------------------------------------------------------------- # Name: configIdAsDict() # Role: Convert a configID into a dictionary # Fix: "name" can include a hyphen. #------------------------------------------------------------------------------- def configIdAsDict( configId ) : 'configIdAsDict( configId ) - Given a configID, return a dictionary of the name/value components.' result = {}; # Result is a dictionary hier = []; # Initialize to simplifiy checks try : # Be prepared for an error #----------------------------------------------------------------- # Does the specified configID match our RegExp pattern? # Note: mo == Match Object, if mo != None, a match was found #----------------------------------------------------------------- if ( configId[ 0 ] == '"' ) and ( configId[ -1 ] == '"' ) and ( configId.count( '"' ) == 2 ) : configId = configId[ 1:-1 ]; mo = re.compile( r'^([-\w ]+)\(([^|]+)\|[^)]+\)$' ).match( configId ); if mo : Name = mo.group( 1 ); hier = mo.group( 2 ).split( '/' ); if mo and ( len( hier ) % 2 == 0 ) : #--------------------------------------------------------------- # hier == Extracted config hierarchy string #--------------------------------------------------------------- for i in range( 0, len( hier ), 2 ) : name, value = hier[ i ], hier[ i + 1 ]; result[ name ] = value; if result.has_key( 'Name' ) : print '''%s: Unexpected situation - "Name" attribute conflict, Name = "%s", Name prefix ignored: "%s"''' % ( funName, result[ 'Name' ], Name ); else : result[ 'Name' ] = Name; else : print '''configIdAsDict: Warning: The specified configId doesn\'t match the expected pattern, and is ignored. configId: "%(configId)s"''' % locals(); except : ( kind, value ) = sys.exc_info()[ :2 ]; print '''configIdAsDict: Unexpected exception.\n Exception type: %(kind)s Exception value: %(value)s''' % locals(); return result; #------------------------------------------------------------------------------- # Name: showAsDict() # Role: Convert result of AdminConfig.show( configID ) to a dictionary #------------------------------------------------------------------------------- def showAsDict( configID ) : 'Convert result of AdminConfig.show( configID ) to a dictionary & return it.' result = {} try : #--------------------------------------------------------------------------- # The result of the AdminConfig.show() should be a string containing many # lines. Each line of which starts and ends with brackets. The "name" # portion should be separated from the associated value by a space. #--------------------------------------------------------------------------- for item in AdminConfig.show( configID ).splitlines() : if ( item[ 0 ] == '[' ) and ( item[ -1 ] == ']' ) : ( key, value ) = item[ 1:-1 ].split( ' ', 1 ) result[ key ] = value except NameError, e : print 'Name not found: ' + str( e ) except : ( kind, value ) = sys.exc_info()[ :2 ] print 'Exception type: ' + str( kind ) print 'Exception value: ' + str( value ) return result #------------------------------------------------------------------------------- # main entry point #------------------------------------------------------------------------------- if ( __name__ == '__main__' ) or ( __name__ == 'main' ) : ListPorts(); else : print 'This script should be executed, not imported.';