Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call a .exe from PL/SQL?

I have given the following grants for permission and calling CMD.EXE appears to work like shown in below code example.

However, I cannot get any output from whoami.exe. I can see that the default directory is C:\app\oramgr\product\12.1.0\dbhome_1\database\ that appears in the JJJ.TXT file. However, the JJJ_WHOAMI.TXT file contains nothing.

Any suggestions?

dbms_java.grant_permission('PWATSON', 'SYS:java.io.FilePermission', 'C:\WINDOWS\SYSTEM32\CMD.EXE', 'execute');
dbms_java.grant_permission('PWATSON', 'SYS:java.io.FilePermission', 'C:\Windows\System32\whoami.exe', 'execute');
dbms_java.grant_permission('PWATSON', 'SYS:java.lang.RuntimePermission', '*', 'writeFileDescriptor');
dbms_java.grant_permission('PWATSON', 'SYS:java.lang.RuntimePermission', '*', 'readFileDescriptor');

dbms_java.grant_permission('PWATSON', 'SYS:java.io.FilePermission', 'C:\Users\pwatson', 'read, write');
dbms_java.grant_permission('PWATSON', 'SYS:java.io.FilePermission', 'C:\Users\pwatson\*', 'read, write');
dbms_java.grant_permission('PWATSON', 'SYS:java.io.FilePermission', 'C:\temp', 'read');

SQL> select util.RUN_CMD('C:\WINDOWS\system32\cmd.EXE /c CD C:\Users\pwatson\src\java ^& ECHO ^%CD^% ^& EXIT 4') as y from dual;
         Y
----------
         4

C:\app\oramgr\product\12.1.0\dbhome_1\DATABASE
SQL> select util.RUN_CMD('C:\WINDOWS\system32\cmd.EXE /c ECHO ^%CD^% >jjj.txt ^& EXIT 7') as y from dual;
         Y
----------
         7

SQL> select util.RUN_CMD('C:\WINDOWS\system32\cmd.EXE /c C:\Windows\System32\whoami.exe >jjj_whoami.txt') as y from dual;
         Y
----------
         0


C:\app\oramgr\product\12.1.0\dbhome_1\database>type JJJ.TXT
C:\app\oramgr\product\12.1.0\dbhome_1\DATABASE

C:\app\oramgr\product\12.1.0\dbhome_1\database>type JJJ_WHOAMI.TXT
like image 267
lit Avatar asked Mar 12 '15 15:03

lit


1 Answers

Maybe Oracle is a nobody when it runs whoami on Windows? Good idea is to change the user who starts the service.


The third call is slightly different from the second. You didn't call for an exit.

Try the below:

select util.RUN_CMD('C:\WINDOWS\system32\cmd.EXE /c C:\Windows\System32\whoami.exe >jjj_whoami.txt ^& EXIT 8') as y from dual;
like image 120
J. Chomel Avatar answered Oct 16 '22 02:10

J. Chomel