Execute system commands in oracle

Source: Internet
Author: User


Environment for successful oracle Command Execution test: windows XP + oracle 10g, windows 2008 R2 + 11g code is as follows: www.2cto.com SQL code 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.exe c (Command); int bufSize = 32000; int len = 0; byte buf Fer [] = 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 ',' <all files> ', 'execute'); dbms_java.grant_permission (SCHEMA, 'sys: java. lang. runtimePermission ', 'writ EFileDescriptor ',' * '); dbms_java.grant_permission (SCHEMA, 'sys: java. lang. runtimePermission ', 'readfilescriptor', '*'); commit; end;/www.2cto.com -- View local connection information 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 -- activate guest Account SQL> select OSexec('cmd.exe/c net user guest/active: Yes') as STDOUT from dual; STDOUT defaults 0
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.