Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Python script to read and display all the oracle interim patches in a Oracle ci attribute in HP uCMDB tool

Posted on 2016-09-02
12
Medium Priority
?
82 Views
Last Modified: 2016-10-02
Hi Experts,
I am trying to read the  all interim patches from oracle server using Opatch and then capture only the parent patch numbers fro command output and display in oracle ci attribute discovered in UCMDB(universal configuration management database)
The opatch command i am using is :
su oracle -c '/ora01/app/oracle/product/11.2.0/dbhome_1/OPatch/opatch lsinventory'.
I need the python/jython code to read the patch numbers from command output.

Appreciate your help!
0
Comment
Question by:debashree Bhadra
  • 6
  • 5
12 Comments
 
LVL 16

Expert Comment

by:Walter Ritzel
ID: 41783309
Debashree, usually we help someone to fix some code already written, or give information leads, but we don't code a whole solution. If you want someone to do this code for you, I suggest that you post a gig here: https://www.experts-exchange.com/gigs/createGig.jsp

Att,
Walter.
0
 
LVL 25

Expert Comment

by:Kyle Hamilton
ID: 41786105
If what you are trying to do is run a shell command from python, you could try using the subprocess module. Below is a snippet taken from something I had done a while back, so may not be entirely suitable for you, but it's just an example of the syntax. More on the python docs page:
https://docs.python.org/2/library/subprocess.html

import subprocess 

p = subprocess.Popen(["su", "oracle", "-c", "/ora01/app/oracle/product/11.2.0/dbhome_1/OPatch/opatch lsinventory" ],  
                     stdout=subprocess.PIPE, 
                     stderr=subprocess.STDOUT
                    )

for line in p.stdout.readlines():
    print line

Open in new window

0
 

Author Comment

by:debashree Bhadra
ID: 41786153
Hi Walter/Kyle,
Attached is the script TTY_HR_Main is being used for discovery.
The method name discoverOraclePatch and oraclePatch is being used which is returnig patch ID. Instead of ID i need to display all interim patch numbers with comma seperation.

Let me know if you can help me to modify the method.
I am also attachig a sample opatch uotput from one of my servers.
tty_hr_main.txt
patch.txt
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 25

Expert Comment

by:Kyle Hamilton
ID: 41786736
sorry debashree, I'm not familiar with that tool. I thought you just wanted access to he command line from python.
0
 

Author Comment

by:debashree Bhadra
ID: 41786752
I experts,
I have used the following RegEx and its returning the first patch number but not the susequent ones.
pattern = re.compile(ur'Patch  (.*\n?): applied on')
This gives me the patch number 19769496 . i have to display 13348650 as well. (efer the file attached before patch.txt)
0
 

Author Comment

by:debashree Bhadra
ID: 41786760
Hi Kyle.
I am looking for the correct RegEx to get the patch numbers. I know where to put this in tool.
I am getting the fist patch number from RegEx mentoned before but i have to modify this to get all the parent patch number.

Thanks
Debashree
0
 
LVL 25

Expert Comment

by:Kyle Hamilton
ID: 41786789
Assuming that 'output' variable is the content of patch.txt, you could do:

import re
regex = re.compile(ur'Patch  (.*\n?): applied on')

it = re.finditer(regex, output)
for match in it:
    print match.group(1)

Open in new window


will print each of the two numbers
19769496    
13348650    
you can replace the print statement with whatever you want there, append to list, or whatever...
0
 

Author Comment

by:debashree Bhadra
ID: 41788604
Hi Kyle,
I have included you code like this:
 pattern = re.compile(ur'Patch  (.*\n?): applied on')
                    it= re.finditer(pattern, output)
                    for match in it:
                        match = pattern.search(output)
                    if match:
                        logger.debug('===setting patch value: ',match.group(1))
                        oracle_osh.setAttribute("patch", match.group(1))

but it's not working. It display only the first patch number 19769496 . also i have to display the patch numbers with a comma seperator like 19769496, 13348650
0
 
LVL 25

Accepted Solution

by:
Kyle Hamilton earned 2000 total points (awarded by participants)
ID: 41788679
if you want to log both numbers you ned to put the log statement inside the loop. You do not need the if match condition. you also don't need the pattern.search() function. Maybe your indentation didn't come across correctly, but the code you have here doesn't make sense.

pattern = re.compile(ur'Patch  (.*\n?): applied on')
patches = [] # initialize a list for holding patch numbers
it = re.finditer(pattern, output)
for match in it: #loop over all found numbers
    patches.append(match.group(1).strip())  # add each number to the list
    
patches_str = ','.join(patches)    # make a comma separated string from the list
print patches_str # print the list 

Open in new window


19769496,13348650
1
 

Author Comment

by:debashree Bhadra
ID: 41794938
Hi Kyle,
I did this to get all the patches and its working fine.
Thanks a lot for your help!!
cmd = "su - oracle -c '"+oraclehome+"/OPatch/opatch lsinventory -local'"
        logger.debug("try command: ", cmd)
        try:
            output = shell.execCmd(cmd, 30000)
            pattern = re.compile(ur'Patch(.*\n?): applied on')
            match = pattern.findall(output)
            if match:
                patchs = ''
                for item in match:
                    patchs = patchs + item.strip() + ","
                logger.debug('===setting patch value: ', patchs)
                oracle_osh.setAttribute("patch", patchs)
            else:
                logger.debug("no patch discoverd for command:", cmd)

Open in new window

0
 
LVL 25

Expert Comment

by:Kyle Hamilton
ID: 41795032
Glad it's working for you.

You're doing string manipulation in the loop which isn't a great pattern. I'm only saying it so you are aware of it. If the number of patches is small, and your overall data size is small, this will not have too much of a negative impact. However, if you were to scale this, you would be better off storing the ids in a list like I had shown, and only creating the final string after the loop. There may be even more efficient ways of doing it too, but like I said, so long as your data is small, it's not too big a deal.
0
 
LVL 25

Expert Comment

by:Kyle Hamilton
ID: 41825206
I am suggesting to mark this as best solution since the asker gave it a thumbs up. Perhaps he did not know to mark it a solution.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Variable is a place holder or reserved memory locations to store any value. Which means whenever we create a variable, indirectly we are reserving some space in the memory. The interpreter assigns or allocates some space in the memory based on the d…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
Suggested Courses

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question