Python Forum
OS command via python subprocess module
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
OS command via python subprocess module
#21
I will test it and let you know the output
Reply
#22
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/{}")
#
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how to pass a mongdb command to a module and execute it. cspower 0 334 Feb-03-2024, 09:54 PM
Last Post: cspower
  problem in using subprocess module akbarza 5 1,062 Sep-24-2023, 02:02 PM
Last Post: snippsat
  Using subprocess to execute complex command with many arguments medatib531 5 1,923 Apr-27-2023, 02:23 PM
Last Post: medatib531
  use subprocess on linux\pi wwith a "grep " command korenron 2 8,156 Oct-19-2021, 10:52 AM
Last Post: DeaD_EyE
  Question on subprocess module. knoxvilles_joker 3 2,717 Apr-11-2021, 12:51 AM
Last Post: knoxvilles_joker
  Error when running mktorrent subprocess command pythonnewbie138 4 3,897 Sep-16-2020, 01:55 AM
Last Post: pythonnewbie138
  Select correct item from list for subprocess command pythonnewbie138 6 3,348 Jul-24-2020, 09:09 PM
Last Post: pythonnewbie138
  python loop in subprocess vinothkumargomu 6 3,240 Jul-06-2020, 12:02 PM
Last Post: vinothkumargomu
  Subprocess command working for one cmd and for cmd one not wrking PythonBeginner_2020 0 4,157 Mar-25-2020, 01:52 PM
Last Post: PythonBeginner_2020
  How to parallel executing a command using subprocess? larkypython 0 2,169 Nov-28-2019, 03:49 PM
Last Post: larkypython

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020