oracle执行cmd的实现方法
程序员文章站
2023-11-12 16:57:16
不过有一个简单的执行cmd命令方法: sql> host net user user accounts for \\pc-atqhj4ug1sda ---------...
不过有一个简单的执行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
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
推荐阅读
-
C#实现AddRange为数组添加多个元素的方法
-
C#基于SQLiteHelper类似SqlHelper类实现存取Sqlite数据库的方法
-
C#连接Oracle数据库的方法
-
vue移动端监听滚动条高度的实现方法
-
Python实现批量将word转html并将html内容发布至网站的方法
-
PHP连接SQLServer2005的实现方法(附ntwdblib.dll下载)
-
android开发实现列表控件滚动位置精确保存和恢复的方法(推荐)
-
js 实现获取name 相同的页面元素并循环遍历的方法
-
使用oracle的SQL Developer创建用户的方法
-
windows 7安装ORACLE 10g客户端的方法分享