WEB开发网
开发学院数据库Oracle 在Oracle运行操作系统命令 阅读

在Oracle运行操作系统命令

 2007-05-07 12:08:13 来源:WEB开发网   
核心提示: 通过Sql*Plus运行以上语句,将为当前用户创建daemon程序包.2 创建在OS上运行的守护进程,监听由上面的daemon程序包发来的要求执行os命令的语句.以下Pro*C的代码,必须由pro*c先进行预编译.#include#includeEXEC SQL INCLUDE SQLCA

通过Sql*Plus运行以上语句,将为当前用户创建daemon程序包.

2 创建在OS上运行的守护进程,监听由上面的daemon程序包发来的要求执行os命令的语句.以下Pro*C的代码,必须由pro*c先进行预编译.

#include
#include
EXEC SQL INCLUDE SQLCA;
EXEC SQL BEGIN DECLARE SECTION;
char *uid = "scott/tiger";/*在这个地方改为你自己访问的用户,密码,服务名*/
int status;
VARCHAR command[20];
VARCHAR value[2000];
VARCHAR return_name[30];
EXEC SQL END DECLARE SECTION;
void
connect_error()
{
char msg_buffer[512];
int msg_length;
int buffer_size = 512;
EXEC SQL WHENEVER SQLERROR CONTINUE;
sqlglm(msg_buffer, &buffer_size, &msg_length);
printf("Daemon error while connecting:
");
printf("%.*s
", msg_length, msg_buffer);
printf("Daemon quitting.
");
exit(1);
}
void
sql_error()
{
char msg_buffer[512];
int msg_length;
int buffer_size = 512;
EXEC SQL WHENEVER SQLERROR CONTINUE;
sqlglm(msg_buffer, &buffer_size, &msg_length);
printf("Daemon error while executing:
");
printf("%.*s
", msg_length, msg_buffer);
printf("Daemon continuing.
");
}
main()
{
EXEC SQL WHENEVER SQLERROR DO connect_error();
EXEC SQL CONNECT :uid;
printf("Daemon connected.
");
EXEC SQL WHENEVER SQLERROR DO sql_error();
printf("Daemon waiting...
");
while (1) {
EXEC SQL EXECUTE
BEGIN
/*接收deamon发来的字符*/
:status := DBMS_PIPE.RECEIVE_MESSAGE('daemon');
IF :status = 0 THEN
/*取出字符*/
DBMS_PIPE.UNPACK_MESSAGE(:command);
END IF;
END;
END-EXEC;
IF (status == 0)
{
command.arr[command.len] = '';
/*如果是stop,该进程就退出*/
IF (!strcmp((char *) command.arr, "STOP"))
{
printf("Daemon exiting.
");
break;
}
ELSE IF (!strcmp((char *) command.arr, "SYSTEM"))
{
EXEC SQL EXECUTE
BEGIN
DBMS_PIPE.UNPACK_MESSAGE(:return_name);
DBMS_PIPE.UNPACK_MESSAGE(:value);
END;
END-EXEC;
value.arr[value.len] = '';
printf("Will execute system command '%s'
", value.arr);
/*运行os命令*/
status = system(value.arr);
EXEC SQL EXECUTE
BEGIN
DBMS_PIPE.PACK_MESSAGE('done');
DBMS_PIPE.PACK_MESSAGE(:status);
:status := DBMS_PIPE.SEND_MESSAGE(:return_name);
END;
END-EXEC;
IF (status)
{
printf
("Daemon error while responding to system command.");
printf(" status: %d
", status);
}
}
ELSE
{
printf
("Daemon error: invalid command '%s' received.
",
command.arr);
}
}
ELSE
{
printf("Daemon error while waiting for signal.");
printf(" status = %d
", status);
}
}
EXEC SQL COMMIT WORK RELEASE;
exit(0);
}

以上代码起名为daemon.pc,用proc预编译:

proc iname=daemon.pc userid=用户名/密码@服务名 sqlcheck=semantics

得到daemon.c,在用c进行编译,注意在NT上要把orasql8.lib加上,否则编译通过,连接没法通过.

3 在服务器上运行daemon.exe

4 在sqlplus运行测试语句:

SQL> variable rv number

SQL> execute :rv := DAEMON.EXECUTE_SYSTEM('ls -la');

PL/SQL 过程已成功完成。

SQL> execute :rv := DAEMON.EXECUTE_SYSTEM('dir');

PL/SQL 过程已成功完成。

SQL>

DBMS_PIPE的用法见oracle的文档.

上一页  1 2 

Tags:Oracle 运行 操作系统

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接