EMCLI Python Script File to Create Users

I’ll give presentations at DOAG 2015 on Tuesday (tomorrow), and at the BGOUG Autumn Conference on Saturday. My presentation will be about EMCLI (Enterprise Manager Command Line Interface). I will try to explain the basics of EMCLI, its terminology, the important points, and how we can use it with bash for scripting. I’ll also speak about EMCLI Python scripting, very short but useful into to Python and then share some sample scripts. Because of the time limit of the session, I need to be picky about sample scripts and put only a few ones (which points important points). I’ll share more sample scripts on my blog. The following script shows how you can write an EMCLI script which will read usernames, passwords and roles from a CSV file and then create these users as EM administrators. This script must be used with “EMCLI with Scripting Mode” (aka advancedkit):

I think the code is self-explanatory. As you can see, the script accepts only one argument (the name of the CSV file containing the information of new users. The arguments can be accessed via sys.argv object. Then it logins to OMS (it will ask the password of SYSMAN. You can modify the script and add your EM administrator username and password. At line 18, it opens the file, at line 24, it starts to loop for each line in the file. We split the string to an array at line 27, and use the members of the array to create new users at line 31.

Here’s the sample CSV file:

By the way, I do not try to share “production quality” code. The sample code is just for guiding you to write better ones (for example: you may surely want to add exception handling etc…). If you have any questions about EMCLI Scripting, send them using contact form. I’ll be happy to answer them.

If you will attend to DOAG 2015 or BGOUG Autumn Conference, hope to see you in my session:
 
DOAG
Jump into the EMCLI: A Crash Course for EM12c Administrators
Date: 2015-11-17 Time: 4:00 pm Room: Tokio
 
BGOUG
Jump into the EMCLI: A Crash Course for EM12c Administrators
Date: 2015-11-21 Time: 10:00 am Room: Hall B

Please share
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Gokhan Atil is a database administrator who has hands-on experience with both RDBMS and noSQL databases, and strong background on software development. He is certified as Oracle Certified Professional (OCP) and is awarded as Oracle ACE (in 2011) and Oracle ACE Director (in 2016) for his continuous contributions to the Oracle users community.

7 Comments

  1. Amritap Datta

    Hi,

    I need to pass user inputs in custom python script for emcli. When i use raw_input or input it is getting hung and not progressing. Is there a way?

    • Gokhan Atil

      Hi Amritap,

      When I try to get user input using raw_input function, it works as it expected. Which version do you use? Can you share the code you wrote (only the related part)?

  2. Ravi kumar Pilla

    Hi Gokhan,

    Thank you for sharing the script.
    I modified your script as below and its working fine, but only 2nd privilege is reflected in “Manage Target Privilege grants”.

    create_user( name=sp[0]
    , password=sp[1]
    , roles=”EM_USER;PUBLIC”
    ,privilege=”CONNECT_READONLY_TARGET;”+sp[2]+”:oracle_database”
    ,privilege=”DB_AWR_VIEW;”+sp[2]+”:oracle_database”
    ,desc=”Target db”)

    Can you help me resolve the issue, so that i could see both the privileges in the grants.

    Regards
    Ravi

    • Gokhan Atil

      Hi Ravi,

      Unfortunately I do not have access to EM right now (I think I’ll create a test environment on Oracle Cloud to answer questions). When I check the help file, It says “you can specify this option more than once in command-line format”. So in scripting mode, I think it only accepts the last one. Although I’m not sure of it but you may try to combine the privileges in one string using “;” between them:

      privilege=”CONNECT_READONLY_TARGET;”+sp[2]+”:oracle_database;DB_AWR_VIEW;”+sp[2]+”:oracle_database”

  3. Ravi kumar Pilla

    Hi Gokhan,

    I tried that earlier, but getting Syntax error

    Traceback (most recent call last):
    File “create_users.py”, line 40, in
    create_user( name=sp[0]
    File “”, line 3, in create_user
    File “/u01/app/oracle/em/middleware/bin/./emsh.jar/jython_lib/emcli/util.py”, line 90, in do_verb_processing
    emcli.exception.VerbExecutionError: Syntax Error: Value “CONNECT_READONLY_TARGET;TMRSTG_dp:oracle_database;DB_AWR_VIEW;TMRSTG_dp:oracle_database” specified by parameter “privilege” contains irrelevant fields
    Logout successful

    Regards
    Ravi

    • Gokhan Atil

      Hi Ravi, then you may use “grant_privs” to grant additional privileges (after creating the user).

Leave Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.