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
Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: