Python Forum

Full Version: OS command via python subprocess module
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3
I will test it and let you know the output
This is my final script. The else part for "apply_patches" is not getting executed

#
!/usr/local/bin/python3.6
import re
import subprocess
from plumbum import local, cmd

def opatch_function():
    x = subprocess.check_output(["opatch", "version"])
    output2 = x.decode("utf-8")

    match = re.findall(r'12.1', output2)
    if match:
        print("Opatch version is :", re.findall(r'\d[^\s]+', output2))
        print("No need to upgrade Opatch")
    else:
        print("Upgarding Opatch")
        ORACLE_HOME = local.env['ORACLE_HOME']
        path = local.env['PATH']
        local.env['PATH'] = "{ohome}/bin:{ohome}/OPatch:{path}".format(ohome=ORACLE_HOME, path=local.env['PATH'])
        local.cwd.chdir(ORACLE_HOME)
        cmd.rm('-rf', 'OPatch.pre6880880')
        cmd.mv('OPatch', 'OPatch.pre6880880')
        cmd.mkdir('OPatch')
        osTyp = cmd.uname('-s').strip()
        print('OPatch Upgraded to version')

        if osTyp == 'Linux':
            cmd.cp("/u03/p6880880_121010_Linux-x86-64.zip", ".")
            cmd.unzip("p6880880_121010_Linux-x86-64.zip")
        elif osTyp == 'SunOs':
            cmd.cp("/u03/p6880880_121010_Linux-x86-64.zip", ".")
            cmd.unzip("p6880880_121010_Linux-x86-64.zip")
        else:
            print("!!\n!! unable to determine OS type !!\n!!")
            
osTyp = cmd.uname('-s').strip()

def apply_patches(msg, patches, zipfile, dir):
    print(msg)
    for j in patches:
        if osTyp == 'Linux':
            local.cwd.chdir('/u02/')
            cmd.unzip('-o', zipfile.format(j))
            local.cwd.chdir(dir.format(j))
            cmd.opatch('apply', '-silent')
            print('Patch' ,j, 'applied')
        elif osTyp == 'SunOs':
            cmd.unzip("p{}_121020_Solaris-x86-64.zip".format(j))
        else:
            print("!!\n!! unable to determine OS type !!\n!!")

          

s = subprocess.check_output(["opatch", "lsinventory"])
output = s.decode("utf-8")


patches = [27923320, 27547329, 21171382, 21463894, 18961555, 28432129]

patches_found = set(re.findall(r'\b(?:%s)\b' % '|'.join(map(str, patches)), output))
patches_missing = set(map(str, patches)) - patches_found

if patches_found:
    print('Patch', patches_found, "detected")

if patches_missing:
    print("Patch", patches_missing, "missing")
    print('Calling Opatch function')
    opatch_function()
    print('Opatch function completed')

    print('Applying Missing Patches')
    if ("27923320","27547329" in patches_missing):
        print("Yes")
        apply_patches('Applying PSU patches', [27923320, 27547329], "/u02/p28317232*.zip", "/u02/28317232/{}")
    else:
         apply_patches('Applying one-off patches', [21171382, 21463894, 18961555, 28432129], "/u02/p{}_*.zip", "/u02/{}")
#
Pages: 1 2 3