oracle执行cmd的实现方法

不过有一个简单的执行cmd命令方法:

sql> host net user

user accounts for \\pc-atqhj4ug1sda

—————————————————————————-

__vmware_user__ admin administrator

aspnet guest iusr_pc-atqhj4ug1sda

iwam_pc-atqhj4ug1sda support_388945a0

the command completed successfully.

unix或linux下用

! command

======================补充======================

网上的另两种方法:


1是利用msvcrt.dll

写一个c:\orac.sql

内容:

rem

rem oracmd.sql

rem

rem run system commands via oracle database servers

rem

rem bugs to david@ngssoftware.com

rem

create or replace library exec_shell as

‘c:\windows\system32\msvcrt.dll’;

/

show errors

create or replace package oracmd is

procedure exec (cmdstring in char);

end oracmd;

/

show errors

create or replace package body oracmd is

procedure exec(cmdstring in char)

is external

name “system” library exec_shell

language c;

end oracmd;

/

show errors

然后c:\>sqlplus /nolog

sql*plus: release 8.1.7.0.0 – production on thu jun 7 14:25:38 2001

(c) copyright 2000 oracle corporation. all rights reserved.

sql> connect system/manager@orcl (分别是用户名密码和sid)

connected.

sql> @c:\orac.sql

library created.

no errors.

package created.

no errors.

package body created.

no errors.

sql>

sql> exec oracmd.exec (‘dir > c:\oracle.txt’);

结果在我本机出现

第 1 行出现错误:

ora-28595: extproc 代理: dll 路径无效

ora-06512: 在 “system.oracmd”, line 2

ora-06512: 在 line 1

没有成功。


第二种方法

c:\1.sql

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;

}

}

}

c:\2.sql

create or replace

function run_cmz(p_cmd in varchar2) return number

as

language java

name ‘util.runthis(java.lang.string) return integer’;

c:\3.sql

create or replace procedure rc(p_cmd in varchar)

as

x number;

begin

x := run_cmz(p_cmd);

end;

登陆上去后依旧是依次执行

sql> @c:\1.sql

/

@c:\2.sql

/

@c:\3.sql

/

variable x number;

set serveroutput on;

exec dbms_java.set_output(100000);

grant javasyspriv to system;

grant javauserpriv to system;(网上的方法没有这一行,我无法成功,加上去可以)

exec :x:=run_cmz(‘ipconfig’); 成功运行了命令

测试环境win2003+oracle11g

(0)
上一篇 2022年3月22日
下一篇 2022年3月22日

相关推荐