| File and operating system interaction with SQL and PLSQL | |||||
SUMMARY: FIELD | TYPE | METHOD | DETAIL: FIELD | TYPE | METHOD |
The OS_COMMAND package contains various functions for executing shell commands from the SQL engine.
The different functions handle STDIN and STDOUT differently. As for the FILE_TYPE object type the
database user needs appropriate privileges in order to execute the shell commands and to access
STDIN and STDOUT. See the function documentation for details.
The following script grants read and write permissions on STDIN and STDOUT and execute
privileges for a specific shell command:
begin -- this grants read privilege on STDIN dbms_java.grant_permission( grantee => 'SCOTT', permission_type => 'SYS:java.lang.RuntimePermission', permission_name => 'readFileDescriptor', permission_action => null ); -- this grants write permission on STDOUT dbms_java.grant_permission( grantee => 'SCOTT', permission_type => 'SYS:java.lang.RuntimePermission', permission_name => 'writeFileDescriptor', permission_action => null ); -- this grants execute privilege for the 'ls -la' command dbms_java.grant_permission( grantee => 'SCOTT', permission_type => 'SYS:java.io.FilePermission', permission_name => '/bin/ls', permission_action => 'execute' ); -- this grants execute privilege for ALL shell commands: VERY DANGEROUS! dbms_java.grant_permission( grantee => 'SCOTT', permission_type => 'SYS:java.io.FilePermission', permission_name => '<<ALL FILES>>', permission_action => 'execute' ); end;Usage examples:
SQL> select os_command.exec_clob('ls -la /') directory_listing from dual;
DIRECTORY_LISTING -------------------------------------------------------------------------------- total 246 drwxr-xr-x 25 root root 4096 Jan 21 09:15 . drwxr-xr-x 25 root root 4096 Jan 21 09:15 .. -rw-r--r-- 1 root root 0 Jan 21 08:56 .autofsck -rw-r--r-- 1 root root 0 Jan 17 17:11 .autorelabel drwxr-xr-x 2 root root 4096 Feb 6 15:03 bin drwxr-xr-x 4 root root 1024 Jan 17 18:18 boot drwxr-xr-x 11 root root 3900 Feb 14 09:40 dev
set serveroutput on declare v_stdout clob; v_return number; begin dbms_lob.createtemporary(v_stdout, true, DBMS_LOB.CALL); v_return := os_command.exec(p_command => 'ls -la /', p_stdout => v_stdout); dbms_output.put_line('Return code: '||v_return); dbms_output.put_line('LOB-Content: '); dbms_output.put_line('*************'); dbms_output.put_line(v_stdout); dbms_lob.freetemporary(v_stdout); end; / Return code: 0 LOB-Content: *********** total 246 drwxr-xr-x 25 root root 4096 Jan 21 09:15 . drwxr-xr-x 25 root root 4096 Jan 21 09:15 .. -rw-r--r-- 1 root root 0 Jan 21 08:56 .autofsck -rw-r--r-- 1 root root 0 Jan 17 17:11
Method Summary | |
| clear_environment()
removes all environment variables from the "custom environment". |
| clear_working_dir()
clears the working directory for the "exec" calls. |
number | exec(p_command varchar2, p_stdin blob)
executes a shell command |
number | exec(p_command varchar2, p_stdin clob)
executes a shell command |
number | exec(p_command varchar2)
executes a shell command |
number | exec(p_command varchar2, p_stdin clob, p_stdout clob)
executes a shell command |
number | exec(p_command varchar2, p_stdin clob, p_stdout blob)
executes a shell command |
number | exec(p_command varchar2, p_stdin blob, p_stdout blob)
executes a shell command |
number | exec(p_command varchar2, p_stdin blob, p_stdout clob)
executes a shell command |
number | exec(p_command varchar2, p_stdout clob)
executes a shell command |
number | exec(p_command varchar2, p_stdout blob)
executes a shell command |
number | exec(p_command varchar2, p_stdin clob, p_stdout clob, p_stderr clob)
executes a shell command |
number | exec(p_command varchar2, p_stdin clob, p_stdout blob, p_stderr blob)
executes a shell command |
number | exec(p_command varchar2, p_stdin blob, p_stdout blob, p_stderr blob)
executes a shell command |
number | exec(p_command varchar2, p_stdin blob, p_stdout clob, p_stderr clob)
executes a shell command |
number | exec(p_command varchar2, p_stdout clob, p_stderr clob)
executes a shell command |
number | exec(p_command varchar2, p_stdout blob, p_stderr blob)
executes a shell command |
blob | exec_BLOB(p_command varchar2, p_stdin blob)
executes a shell command |
blob | exec_BLOB(p_command varchar2, p_stdin clob)
executes a shell command |
blob | exec_BLOB(p_command varchar2)
executes a shell command |
clob | exec_CLOB(p_command varchar2, p_stdin blob)
executes a shell command |
clob | exec_CLOB(p_command varchar2, p_stdin clob)
executes a shell command |
clob | exec_CLOB(p_command varchar2)
executes a shell command |
varchar2 | get_env_var(p_env_name varchar2)
gets the value of the specified environment variable from the custom environment. |
varchar2 | get_shell()
returns the operating system call for the current shell. |
FILE_TYPE | get_working_dir()
gets the current working directory setting; NULL if no working directory was set. |
| load_env()
copies the default environment (in which the "oracle" process runs) into the custom environment. |
| load_env(p_env_name varchar2)
copies the specified environment variable from the default environment (in which the "oracle" process runs) into the custom environment. |
| remove_env_var(p_env_name varchar2)
removes an environment variable from the custom environment. |
| set_env_var(p_env_name varchar2, p_env_value varchar2)
sets an environment variable in the custom environment. |
| set_exec_direct()
Execute operating system commands (exec procedures and functions) directly. |
| set_exec_in_shell()
Execute operating system commands (exec procedures and functions) in a shell. |
| set_Shell(p_shell_path varchar2, p_shell_switch varchar2)
sets the path and "command switch" for the shell to use if set_exec_in_shell was called. |
| set_working_dir(p_workdir file_type)
sets the working directory for the "exec" calls. |
| use_custom_env()
Use "custom" environment variables defined by this package for the "exec" calls. |
| use_default_env()
Use "default" environment variables defined by this package for the "exec" calls. |
Method Detail |
public set_working_dir(p_workdir file_type)
p_workdir
-
the working directory to use as FILE_TYPE.public clear_working_dir()
public FILE_TYPE get_working_dir()
public clear_environment()
public set_env_var(p_env_name varchar2,
p_env_value varchar2)
p_env_name
-
the name of the environment variable.p_env_value
-
the value for this environment variable.public remove_env_var(p_env_name varchar2)
p_env_name
-
the name of the environment variable.public varchar2 get_env_var(p_env_name varchar2)
p_env_name
-
the name of the environment variable.public load_env()
getenv.*
) is needed in order to execute this.public load_env(p_env_name varchar2)
p_env_name
-
the name of the environment variable to copy.public use_custom_env()
public use_default_env()
public set_Shell(p_shell_path varchar2,
p_shell_switch varchar2)
OS_COMMAND.SET_SHELL('/bin/sh', '-c');On Windows:
OS_COMMAND.SET_SHELL('C:\WINDOWS\SYSTEM32\CMD.EXE', '/C');These are also the default settings.
p_shell_path
-
path to the shell executable e.g. "/bin/sh"p_shell_switch
-
shell executable switch to execute a command e.g. "-c"public varchar2 get_shell()
select OS_COMMAND.GET_SHELL shell from dual; SHELL ------ /bin/sh -c
public set_exec_in_shell()
begin OS_COMMAND.SET_EXEC_IN_SHELL; end; / select os_command.exec_clob('/bin/ls -la /usr/lib | /usr/bin/grep .so | /usr/bin/wc -l') complex_command from dual; COMPLEX_COMMAND --------------- 345
public set_exec_direct()
begin OS_COMMAND.SET_EXEC_DIRECT; end; / select os_command.exec_clob('/bin/ls -la /usr/lib | /usr/bin/grep .so | /usr/bin/wc -l') complex_command from dual; COMPLEX_COMMAND --------------------------------------------------------------- Error during creation of the process begin OS_COMMAND.SET_EXEC_IN_SHELL; end; / select os_command.exec_clob('/bin/ls -la /usr/lib | /usr/bin/grep .so | /usr/bin/wc -l') complex_command from dual; COMPLEX_COMMAND --------------- 345
public clob exec_CLOB(p_command varchar2,
p_stdin blob)
p_command
-
the command string to executep_stdin
-
binary content (BLOB) to be passed to STDINpublic clob exec_CLOB(p_command varchar2,
p_stdin clob)
p_command
-
the command string to executep_stdin
-
character content (CLOB) to be passed to STDINpublic blob exec_BLOB(p_command varchar2,
p_stdin blob)
p_command
-
the command string to executep_stdin
-
binary content (BLOB) to be passed to STDINpublic blob exec_BLOB(p_command varchar2,
p_stdin clob)
p_command
-
the command string to executep_stdin
-
character content (CLOB) to be passed to STDINpublic clob exec_CLOB(p_command varchar2)
p_command
-
the command string to executepublic blob exec_BLOB(p_command varchar2)
p_command
-
the command string to executepublic number exec(p_command varchar2,
p_stdin blob)
p_command
-
the command string to executep_stdin
-
binary content (BLOB) to be passed to STDINpublic number exec(p_command varchar2,
p_stdin clob)
p_command
-
the command string to executep_stdin
-
character content (CLOB) to be passed to STDINpublic number exec(p_command varchar2)
p_command
-
the command string to executepublic number exec(p_command varchar2,
p_stdin clob,
p_stdout clob)
p_command
-
the command string to executep_stdin
-
character content (CLOB) to be passed to STDINp_stdout
-
OUT Parameter: content written by the shell command to STDOUT as CLOB. This must be a valid LOB locator (persistent or temporary LOB)public number exec(p_command varchar2,
p_stdin clob,
p_stdout blob)
p_command
-
the command string to executep_stdin
-
character content (CLOB) to be passed to STDINp_stdout
-
OUT Parameter: content written by the shell command to STDOUT as BLOB. This must be a valid LOB locator (persistent or temporary LOB)public number exec(p_command varchar2,
p_stdin blob,
p_stdout blob)
p_command
-
the command string to executep_stdin
-
binary content (BLOB) to be passed to STDINp_stdout
-
OUT Parameter: content written by the shell command to STDOUT as BLOB. This must be a valid LOB locator (persistent or temporary LOB)public number exec(p_command varchar2,
p_stdin blob,
p_stdout clob)
p_command
-
the command string to executep_stdin
-
binary content (BLOB) to be passed to STDINp_stdout
-
OUT Parameter: content written by the shell command to STDOUT as CLOB. This must be a valid LOB locator (persistent or temporary LOB)public number exec(p_command varchar2,
p_stdout clob)
p_command
-
the command string to executep_stdout
-
OUT Parameter: content written by the shell command to STDOUT as CLOB. This must be a valid LOB locator (persistent or temporary LOB)public number exec(p_command varchar2,
p_stdout blob)
p_command
-
the command string to executep_stdout
-
OUT Parameter: content written by the shell command to STDOUT as BLOB. This must be a valid LOB locator (persistent or temporary LOB)public number exec(p_command varchar2,
p_stdin clob,
p_stdout clob,
p_stderr clob)
p_command
-
the command string to executep_stdin
-
content to be passed to STDINp_stdout
-
CLOB to store STDOUT output in - must be a valid LOB locator (persistent or temporary LOB)p_stderr
-
CLOB to store STDERR output in - must be a valid LOB locator (persistent or temporary LOB)public number exec(p_command varchar2,
p_stdin clob,
p_stdout blob,
p_stderr blob)
p_command
-
the command string to executep_stdin
-
content to be passed to STDINp_stdout
-
BLOB to store STDOUT output in - must be a valid LOB locator (persistent or temporary LOB)p_stderr
-
BLOB to store STDERR output in - must be a valid LOB locator (persistent or temporary LOB)public number exec(p_command varchar2,
p_stdin blob,
p_stdout blob,
p_stderr blob)
p_command
-
the command string to executep_stdin
-
content to be passed to STDINp_stdout
-
BLOB to store STDOUT output in - must be a valid LOB locator (persistent or temporary LOB)p_stderr
-
BLOB to store STDERR output in - must be a valid LOB locator (persistent or temporary LOB)public number exec(p_command varchar2,
p_stdin blob,
p_stdout clob,
p_stderr clob)
p_command
-
the command string to executep_stdin
-
content to be passed to STDINp_stdout
-
CLOB to store STDOUT output in - must be a valid LOB locator (persistent or temporary LOB)p_stderr
-
CLOB to store STDERR output in - must be a valid LOB locator (persistent or temporary LOB)public number exec(p_command varchar2,
p_stdout clob,
p_stderr clob)
p_command
-
the command string to executep_stdout
-
CLOB to store STDOUT output in - must be a valid LOB locator (persistent or temporary LOB)p_stderr
-
CLOB to store STDERR output in - must be a valid LOB locator (persistent or temporary LOB)public number exec(p_command varchar2,
p_stdout blob,
p_stderr blob)
p_command
-
the command string to executep_stdout
-
BLOB to store STDOUT output in - must be a valid LOB locator (persistent or temporary LOB)p_stderr
-
BLOB to store STDERR output in - must be a valid LOB locator (persistent or temporary LOB)
| File and operating system interaction with SQL and PLSQL | |||||
SUMMARY: FIELD | TYPE | METHOD | DETAIL: FIELD | TYPE | METHOD |