Write the contents out to a CSV file
import sys import cx_Oracle import os def printf (format,*args): sys.stdout.write (format % args) def printException (exception): error, = exception.args printf ("Error code = %s\n",error.code); printf ("Error message = %s\n",error.message); username = sys.argv[1] password = sys.argv[2] databaseName = sys.argv[3] new_user = sys.argv[4] new_password = sys.argv[5] print('Connecting to ' + databaseName) try: connection = cx_Oracle.connect (username,password,databaseName) except cx_Oracle.DatabaseError as e: error, = e.args printf ('Failed to connect to %s\n',databaseName) printException (exception) cursor = connection.cursor () try: print('Changing account password.....') cursor.execute ("alter user " + new_user + " identified by " + new_password) except cx_Oracle.DatabaseError as e: error, = e.args printf ('Failed to get host and instance_name\n') printException (exception) exit (1) print('Password changed for USER') exit(0)
Write the output to a csv file.
#!/usr/bin/python # Example of fetchone import sys import cx_Oracle def printf (format,*args): sys.stdout.write (format % args) def printException (exception): error, = exception.args printf ("Error code = %s\n",error.code); printf ("Error message = %s\n",error.message); username = 'system' password = 'testpassword' databaseName = sys.argv[1] try: connection = cx_Oracle.connect (username,password,databaseName) except cx_Oracle.DatabaseError as e: error, = e.args printf ('Failed to connect to %s\n',databaseName) printException (exception) exit (1) cursor = connection.cursor () try: cursor.execute ("select * from dba_role_privs where granted_role='DBA'") except cx_Oracle.DatabaseError as e: error, = e.args printf ('Failed to get host and instance_name\n') printException (exception) exit (1) for result in cursor: f = open('user_audit.csv', 'a') f.write(databaseName) f.write('\n') f.write(str(result)) f.write('\n') f.write('--------------------------------------------------------------\n') f.write('\n') f.close() cursor.close () connection.close () exit (0)