execute o/s commands from Oracle SQL Prompt



create or replace and compile
    java source named "Util"
    as
    import java.io.*;
    import java.lang.*;
    
    public class Util extends Object
    {
      public static int RunThis(String args)
     {
     Runtime rt = Runtime.getRuntime();
     int        rc = -1;
   
     try
     {
        Process p = rt.exec(args);
   
        int bufSize = 4096;
        BufferedInputStream bis =
         new BufferedInputStream(p.getInputStream(), bufSize);
        int len;
        byte buffer[] = new byte[bufSize];
   
        // Echo back what the program spit out
        while ((len = bis.read(buffer, 0, bufSize)) != -1)
           System.out.write(buffer, 0, len);
   
        rc = p.waitFor();
     }
     catch (Exception e)
     {
        e.printStackTrace();
        rc = -1;
     }
     finally
     {
        return rc;
     }
     }
   }
/


create or replace
   function RUN_CMD( p_cmd  in varchar2) return number
   as
   language java
   name 'Util.RunThis(java.lang.String) return integer';
/
 
create or replace procedure RC(p_cmd in varchar2)   
 as     x number;  
 begin     
 x := run_cmd(p_cmd);   
 end;
/
 
on linux server check where ls command executable is located
 
$ which ls
/bin/ls
 
 
Conn as sys 
 
 begin         
 dbms_java.grant_permission('CELLPAY','java.io.FilePermission','/bin/ls','execute'); 
 dbms_java.grant_permission('CELLPAY','java.lang.RuntimePermission','*','writeFileDescriptor' ); 
 end; 
/
 

PL/SQL procedure successfully completed.
Now check the command 
set serveroutput on size 1000000
exec dbms_java.set_output(1000000)
 
exec rc('/bin/ls /usr')
 
----
 
for other commands
 
begin        
dbms_java.grant_permission ('PORTALPAY','java.io.FilePermission','/apps/ping.sh','execute');                  
dbms_java.grant_permission  ('PORTALPAY','java.io.FilePermission','/bin/date','execute');          
dbms_java.grant_permission  ('PORTALPAY','java.io.FilePermission','/usr/bin/ssh','execute');         
dbms_java.grant_permission  ('PORTALPAY','java.io.FilePermission','/bin/echo','execute');         
dbms_java.grant_permission  ('PORTALPAY','java.io.FilePermission','/bin/chmod','execute');                
dbms_java.grant_permission  ('PORTALPAY', 'java.io.FilePermission','/bin/ping','execute');            
dbms_java.grant_permission  ('PORTALPAY', 'java.io.FilePermission','/apps','write');             
dbms_java.grant_permission ('PORTALPAY', 'java.lang.RuntimePermission','*','writeFileDescriptor' );   
end;
/ 


Post a Comment

And that's all there is to it!

If anyone has any other questions or requests for future How To posts, you can either ask them in the comments or email me. Please don't feel shy at all!

I'm certainly not an expert, but I'll try my hardest to explain what I do know and research what I don't know.

Previous Post Next Post