Execute Command in Operating System Using Java Source
Autor: Denilson Nascimento | Publicado: 2013-06-24T15:16:00-03:00


--As &nbsp;SYS Schema

EXEC dbms_java.grant_permission('DENILSON', 'SYS:java.lang.RuntimePermission', 'writeFileDescriptor', '');
EXEC dbms_java.grant_permission('DENILSON', 'SYS:java.lang.RuntimePermission', 'readFileDescriptor', '');
EXEC dbms_java.grant_permission('DENILSON', 'SYS:java.io.FilePermission', '/bin/sh', 'execute');

connect denilson/denilson

CREATE OR REPLACE AND COMPILE JAVA SOURCE NAMED "host" AS
import java.io.*;
public class Host {
&nbsp; public static void executeCommand(String command) {
&nbsp; &nbsp; try {
&nbsp; &nbsp; &nbsp; String[] finalCommand;
&nbsp; &nbsp; &nbsp; if (System.getProperty("os.name").toLowerCase().indexOf("windows") != -1) {
&nbsp; &nbsp; &nbsp; &nbsp; finalCommand = new String[4];
&nbsp; &nbsp; &nbsp; &nbsp; finalCommand[0] = "C:\\winnt\\system32\\cmd.exe";
&nbsp; &nbsp; &nbsp; &nbsp; finalCommand[1] = "/y";
&nbsp; &nbsp; &nbsp; &nbsp; finalCommand[2] = "/c";
&nbsp; &nbsp; &nbsp; &nbsp; finalCommand[3] = command;
&nbsp; &nbsp; &nbsp; } else { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Linux or Unix System
&nbsp; &nbsp; &nbsp; &nbsp; finalCommand = new String[3];
&nbsp; &nbsp; &nbsp; &nbsp; finalCommand[0] = "/bin/sh";
&nbsp; &nbsp; &nbsp; &nbsp; finalCommand[1] = "-c";
&nbsp; &nbsp; &nbsp; &nbsp; finalCommand[2] = command;
&nbsp; &nbsp; &nbsp; }

&nbsp; &nbsp; &nbsp; // Execute the command...
&nbsp; &nbsp; &nbsp; final Process pr = Runtime.getRuntime().exec(finalCommand);

&nbsp; &nbsp; &nbsp; // Capture output from STDOUT...
&nbsp; &nbsp; &nbsp; BufferedReader br_in = null;
&nbsp; &nbsp; &nbsp; try {
&nbsp; &nbsp; &nbsp; &nbsp; br_in = new BufferedReader(new InputStreamReader(pr.getInputStream()));
&nbsp; &nbsp; &nbsp; &nbsp; String buff = null;
&nbsp; &nbsp; &nbsp; &nbsp; while ((buff = br_in.readLine()) != null) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("stdout: " + buff);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {Thread.sleep(100); } catch(Exception e) {}
&nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; br_in.close();
&nbsp; &nbsp; &nbsp; } catch (IOException ioe) {
&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Error printing process output.");
&nbsp; &nbsp; &nbsp; &nbsp; ioe.printStackTrace();
&nbsp; &nbsp; &nbsp; } finally {
&nbsp; &nbsp; &nbsp; &nbsp; try {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; br_in.close();
&nbsp; &nbsp; &nbsp; &nbsp; } catch (Exception ex) {}
&nbsp; &nbsp; &nbsp; }

&nbsp; &nbsp; &nbsp; // Capture output from STDERR...
&nbsp; &nbsp; &nbsp; BufferedReader br_err = null;
&nbsp; &nbsp; &nbsp; try {
&nbsp; &nbsp; &nbsp; &nbsp; br_err = new BufferedReader(new InputStreamReader(pr.getErrorStream()));
&nbsp; &nbsp; &nbsp; &nbsp; String buff = null;
&nbsp; &nbsp; &nbsp; &nbsp; while ((buff = br_err.readLine()) != null) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("stderr: " + buff);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {Thread.sleep(100); } catch(Exception e) {}
&nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; br_err.close();
&nbsp; &nbsp; &nbsp; } catch (IOException ioe) {
&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Error printing execution errors.");
&nbsp; &nbsp; &nbsp; &nbsp; ioe.printStackTrace();
&nbsp; &nbsp; &nbsp; } finally {
&nbsp; &nbsp; &nbsp; &nbsp; try {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; br_err.close();
&nbsp; &nbsp; &nbsp; &nbsp; } catch (Exception ex) {}
&nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; }
&nbsp; &nbsp; catch (Exception ex) {
&nbsp; &nbsp; &nbsp; System.out.println(ex.getLocalizedMessage());
&nbsp; &nbsp; }
&nbsp; }

};
/

-- Create Function to Call Java Source
CREATE OR REPLACE FUNCTION command(p_command &nbsp; IN &nbsp; VARCHAR2) RETURN VARCHAR2
&nbsp; AS LANGUAGE JAVA NAME
&nbsp; &nbsp; 'Host.executeCommand (java.lang.String) return java.lang.String';

-- Execute Command
CALL DBMS_JAVA.SET_OUTPUT(1000000);
SET SERVEROUTPUT ON SIZE 1000000;
select&nbsp;command('/bin/ls -ltr') from dual;