Oracle Weblogic password reset script

If you want to reset the ‘GOD’ password for Weblogic Server

Start the script and follow instructions

  <path_to_wlst>/wlst.sh resetWLS_password.py

Code for resetWLS_password.py

import datetime
import os
import random
import re
import string
import sys
import traceback

def tempname():
    return "/tmp/resetPassLog" + str(random.randint(1000, 9999))

def shout(cmd, check_returncode=False, check_output=False):
    tmpfile = tempname()
    retval = os.system(cmd + " >" + tmpfile + " 2>&1")
    file = open(tmpfile)
    out = file.read()
    file.close()
    os.system("rm -f " + tmpfile + " >/dev/null 2>&1")
    if check_returncode:
        if retval <> 0:
            print string.strip(out)
            raise Exception("Error: return code " + str(retval) + " from command: " + cmd)
    if check_output:
        if re.search(('ERROR|[Ee]rror|FAIL|[Ff]ail|TIMEOUT|[Tt]imeout'), out):
            print string.strip(out)
            raise Exception("Error message found from command: " + cmd)
    return string.strip(out)

def shlines(cmd):
    return string.split(shout(cmd), "\n")

# RETURN LIST OF PROCESS IDS FROM GREPING PROCESS COMMANDS
def psgrep(str):
    pat = re.compile(str)
    cmd = "ps -e -o pid,command"
    lines = shlines("ps -e -o pid,command")
    pids = []
    for line in lines:
        cols = string.split(line)
        if cols[1] == "grep": continue
        for col in cols[1:]:
            if pat.search(col):
                pids.append(cols[0])
                continue
    return pids
def stop_process_tree(ppid):
    java.lang.Thread.sleep(1)
    pids = get_child_processes(ppid)
    while len(pids) > 0:
        for pid in pids:
            stop_process_tree(pid)
        pids = get_child_processes(ppid)
    kill_process(ppid)
    return

def kill_process(pid):
    if pid == None or pid == '':
        return
    java.lang.Thread.sleep(1)
    if shout("ps -o pid= --pid " + pid) != '':
        os.system("kill " + pid)
    for i in range(30):
        java.lang.Thread.sleep(1)
        if shout("ps -o pid= --pid " + pid) == '':
            return
    os.system("kill -9 " + pid)
    for i in range(30):
        java.lang.Thread.sleep(1)
        if shout("ps -o pid= --pid " + pid) == '':
            return
    error("Cannot kill " + pid)
    return

def get_child_processes(ppid):
    return string.split(shout("ps -o pid= --ppid " + ppid))

#Stop adminserver
def stop_adminserver(name, domain_home):
    print "stopping adminserver " + name
    pids = psgrep("weblogic.Name=" + name)
    for pid in pids:
        stop_process_tree(pid)
    pids = psgrep(domain_home + "/startWebLogic.sh")
    for pid in pids:
        stop_process_tree(pid)
    pids = psgrep(domain_home + "/pointbase.ini")
    for pid in pids:
        stop_process_tree(pid)
    lok = domain_home + "/servers/" + name + "/tmp/" + name + ".lok"
    os.system("rm -f " + lok)
    print "stopped  adminserver " + name
    return ""

def start_adminserver(name, domain_home):
    if len(psgrep(domain_home + "/startWebLogic.sh")) > 0:
        print "adminserver " + name + " already running"
        return ""
    print "starting adminserver " + name
    scriptfile = domain_home + "/startAdminServer.sh"
    outfile = domain_home + "/nohup.out"
    file = open(scriptfile, "w")
    file.write("#!/bin/bash\n")
    file.write(domain_home + "/startWebLogic.sh >" + outfile + " 2>&1 &\n")
    file.close()
    os.system("rm -f " + outfile)
    os.system("chmod 0755 " + scriptfile)
    os.system(scriptfile)
    n = 1900
    while n > 0:
        n = n - 1
        java.lang.Thread.sleep(1)
        file = open(outfile, "r")
        text = file.read()
        file.close()
        if string.find(text, "Server state changed to RUNNING") > -1:
            print "started  adminserver " + name
            return ""
        if string.find(text, "Server state changed to FAILED") > -1:
            errmsg = "ERROR STARTING ADMIN SERVER " + name
            print errmsg
            return errmsg
    errmsg = "ERROR: timeout starting " + name
    return errmsg

##############################
print "Please enter domain data: "
now = datetime.datetime.now()
adminUser = raw_input("Enter Admin user: ")
oldAdminPassword = raw_input("Enter OLD password: ")
newAdminPassword = raw_input("Enter NEW password: ")
url = raw_input("Please, enter url (t3 or http): ")

print "Connection to Oracle Weblogic Adminserver"
connect(adminUser, oldAdminPassword, url)
domainName=cmo.getName()
domainHome = cmo.getRootDirectory()
adminServerName = cmo.getAdminServerName()

cd('/SecurityConfiguration/'+domainName+'/Realms/myrealm/AuthenticationProviders/DefaultAuthenticator')
cmo.resetUserPassword(adminUser,newAdminPassword)
disconnect()
print "Adminpassword succesfully changed"
print "Connecting Using New Credentials....."
connect(adminUser,newAdminPassword,url)
print "Successfully Connected Using New Credentials !!!!"
disconnect()

print " Resetting boot.properties "
bootfile = domainHome + "/boot.properties"
f = open(bootfile, "w")
bootfilestr = "Password file changed by reset adminpass script on " + str(now) + "\n"
bootfilestr += "username=" + adminUser + "\n"
bootfilestr += "password=" + newAdminPassword
f.write(bootfilestr)
f.close()
print "Boot.properties succesfully set"
stop_adminserver(adminServerName, domainHome)
os.system("rm " + domainHome + "/servers/" + adminServerName + "/security/boot.properties")
os.system("cp " + bootfile + " " + domainHome + "/servers/" + adminServerName + "/security/")
start_adminserver(adminServerName, domainHome)
Advertisements

Java -xmx -xms total memory set in bash

Use following commands to know how much memory all java processes take of your Linux server with bash. Handy for Oracle Weblogic administrators:

MINIMUM

[oracle@heuring.com ~]$ ps -ef | grep -oh 'Xms[0-9]*' | cut -d's' -f2 | awk '{SUM += $1} END { print SUM}'

MAXIMUM

[oracle@heuring.com ~]$ ps -ef | grep -oh 'Xmx[0-9]*' | cut -d'x' -f2 | awk '{SUM += $1} END { print SUM}'

Ā 

Oracle Weblogic mail sessions

Sometimes it’s handy to send mail reports from your newly created application… If we choose to do so and we have the expensive obligation of using $$ Oracle Weblogic in our project, we sure can use it šŸ™‚

You need two things:

  1. A J2EE java application with JavaMail
  2. A Weblogic domain which is the container for the application

1. The java snippet


public class RepportMailArchiveerder implements IRapportArchiveerder{
    private static final Logger logger = Logger.getLogger(RepportMailArchiveerder.class);

    public RepportMailArchiveerder() {
        super();
    }

    /**
     * Send email.
     */
    public void mail(Rapport rapport){

        String content = rapport.toString();

        try {
            // Lookup the WLS context
            InitialContext ic = new InitialContext();
            Session session = (Session) ic.lookup("myTestMailSetup");

            //Construct the email
            MimeMessage msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress(session.getProperty("mail.from")));
          msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(session.getProperty("mail.to"), false));
            msg.setSubject(session.getProperty("mail.subject"));
            msg.setText(content);
            Transport.send(msg);
        } catch (MessagingException mex) {
            logger.error("Send mail error: " + mex);
            } catch (NamingException ne){
                ne.getMessage();
            }
    }
}

2. The WLS configuration : SERVICES >> MAIL SESSIONS


Oracle Weblogc config.sh

The config.sh script from Oracle weblogic to create a domain is not compatibel with a Java version > 1.6.0_27 if you like to use X-window. Also the config_builder.sh to create domains is incompatibel with the newest (currentĀ dateĀ 10-11-2011) Java version jdk1.7.0_01.

To fix this, you have to replace the JAVA_HOME var in following Weblogic files (if you installed with jdk1.7.0_01) with your oldie jdk :

  1. commEnv.sh in WL_HOME/common/bin
  2. .product.properties in WL_HOME

To make sure before you start the config tools do this:


export JAVA_HOME=/<path>/<your new jdk>
export PATH=$JAVA_HOME/bin:$PATH
[thomas@oel5505 bin]$ java -version
java version "1.6.0_21"
Java(TM) SE Runtime Environment (build 1.6.0_21-b51)
Java HotSpot(TM) Server VM (build 17.0-b17, mixed mode)
[thomas@oel5505 bin]$ ./config.sh