Oracle
 sql >> Teknologi Basis Data >  >> RDS >> Oracle

Cara mengambil informasi host, port, sid, pengguna dan kata sandi di java.sql.Connection

SID SESI:

SELECT sys_context('USERENV', 'SID') FROM DUAL;

ORACLE_SID:

SELECT sys_context('userenv','instance_name') FROM dual;

HOST (Mesin database) :

SELECT UTL_INADDR.get_host_name FROM dual;

Sandi:Lihat http://www.dba-Oracle.com/t_password_storage.htm

Port SISI KLIEN:

Select port from v$session;

SERVER OS PID dari proses server Oracle terhubung ke proses klien

SELECT p.spid
FROM   v$process p, v$session s
WHERE s.paddr = p.addr and 
sys_context('USERENV', 'SID') = s.sid;

Port sisi server:

Ini sangat sulit karena port proses server dipetakan ke port yang berbeda dari port awal - misalnya port 12102 di listener.ora/tnsnames.ora dipetakan oleh pendengar ke port yang bebas sewenang-wenang (omong-omong:itulah alasan mengapa sering firewall perlu dimatikan dan sementara koneksi tidak dapat diperoleh setelah pemetaan melalui firewall. Ini dapat diperbaiki tetapi ini adalah cerita lain)

Kode di bawah ini

  • memberikan hak istimewa yang diperlukan

  • mendefinisikan fungsi tersimpan java "Util.RunThis" yang mengeksekusi di server database perintah OS yang diteruskan ke fungsi dan mengembalikan string.

  • "Util.RunThis" dipetakan ke fungsi PL/SQL "RUN_CMD"

  • "GET_PORT" adalah fungsi PL/SQL yang mengembalikan nilai numerik dari Port yang digunakan dari proses server database yang terhubung ke sesi klien. Di dalam GET_PORT SELECT digunakan untuk menentukan pid proses server database menggantikan [SPID] pada perintah di bawah ini

      /usr/sbin/lsof -Pan -p [SPID] -i
    
  • akhirnya memanggil pilihan sederhana, kami mendapatkan proses Port server database yang dilampirkan ke sesi saat ini

      connect / as sysdba
    
      grant select on sys.v_$process to scott;        
      grant select on sys.v_$session to scott;
    
      begin
        dbms_java.grant_permission
            ('SCOTT',
             'SYS:java.io.FilePermission',
             '<<ALL FILES>>',
             'execute'); 
      end;
      /
    
      connect scott/[email protected]
    
      create or replace and compile java source named "Util"
      as
       import java.io.*;
       import java.lang.*;
       import java.nio.charset.Charset;
       import java.nio.*;
       public class Util extends Object
       {
         public static String RunThis(String args)
         {
           Runtime rt = Runtime.getRuntime();
           String rc = args;
    
           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)
             {
               String xxx = new String(buffer, Charset.forName("UTF-8"));
               rc = rc + xxx;
             }
             p.waitFor();
    
             rc = rc + "ABC";
    
           }
           catch (Exception e)
           {
             e.printStackTrace();
             rc = "Exception!!!" ;
           }
    
           finally
           {
             return rc;
           }
         }
       }
      /
    
      create or replace function RUN_CMD( p_cmd in varchar2) return VARCHAR2
      as language java
      name 'Util.RunThis(java.lang.String) return java.lang.String';
      /
    
      create or replace function GET_PORT return number
      as
        SPID NUMBER;
        retval varchar2(32000);
        y varchar2(1024);
        cmd VARCHAR2(256);
      begin
        SELECT 
          p.spid 
        INTO 
          SPID
        FROM   
          sys.v_$process p, 
          sys.v_$session s
        WHERE 
          s.paddr = p.addr and 
          sys_context('USERENV', 'SID') = s.sid;
    
        cmd :=  '/usr/sbin/lsof -Pan -p [SPID] -i';
    
        /* raw string data returned from Shell executing cmd */
        retval := run_cmd(replace(cmd,'[SPID]', SPID));
    
        /* get last occurance of : marking redirected port */
        y := substr(retval,INSTR(retval,':', -1)+1,1024);
    
        /* return the numeric port by stripping info like " (ESTABLISHED)" */
        return to_number(substr(y,1,INSTR(y, ' ')-1));
      end;
      /
      show errors
    
      select get_port from dual;
    
      /*-------------------- OUTPUT -------------------------- */
    
      SQL> connect / as sysdba
    
      grant select on sys.v_$process to scott;
    
      grant select on sys.v_$session to scott;
    
      begin
        dbms_java.grant_permission
            ('SCOTT',
             'SYS:java.io.FilePermission',
             '<<ALL FILES>>',
             'execute');
      end;
      /Connected.
      SQL> SQL>
      Grant succeeded.
    
      SQL> SQL>
      Grant succeeded.
    
      SQL> SQL>   2    3    4    5    6    7    8
    
      PL/SQL procedure successfully completed.
    
      SQL> connect scott/[email protected]
    
      Connected.
      SQL> create or replace and compile java source named "Util"
        2  as
       import java.io.*;
       import java.lang.*;
       import java.nio.charset.Charset;
        3    4    5    6   import java.nio.*;
        7   public class Util extends Object
        8   {
        9     public static String RunThis(String args)
         {
           Runtime rt = Runtime.getRuntime();
       10   11   12       String rc = args;
       13
       14       try
       15       {
       16
       17         Process p = rt.exec(args);
    
       18   19         int bufSize = 4096;
       20         BufferedInputStream bis =
       21         new BufferedInputStream(p.getInputStream(), bufSize);
       22         int len;
             byte buffer[] = new byte[bufSize];
       23   24
             // Echo back what the program spit out
             while ((len = bis.read(buffer, 0, bufSize)) != -1)
       25   26   27         {
       28           String xxx = new String(buffer, Charset.forName("UTF-8"));
       29           rc = rc + xxx;
       30         }
             p.waitFor();
       31   32
             rc = rc + "ABC";
    
       33   34   35       }
       36       catch (Exception e)
       37       {
       38         e.printStackTrace();
       39         rc = "Exception!!!" ;
       40       }
       41
       42       finally
       43       {
       44         return rc;
       45       }
         }
       46   47   }
      / 48
    
      Java created.
    
      SQL> create or replace function RUN_CMD( p_cmd in varchar2) return VARCHAR2
      as language java
      name 'Util.RunThis(java.lang.String) return java.lang.String';
      /  2    3    4
    
      Function created.
    
      SQL> create or replace function GET_PORT return number
      as
        SPID NUMBER;
        retval varchar2(32000);
        2    3    4    5    y varchar2(1024);
        cmd VARCHAR2(256);
      begin
        6    7    8    SELECT
          p.spid
        INTO
        9   10   11      SPID
        FROM
          sys.v_$process p,
          sys.v_$session s
        WHERE
       12   13   14   15   16      s.paddr = p.addr and
          sys_context('USERENV', 'SID') = s.sid;
    
        cmd :=  '/usr/sbin/lsof -Pan -p [SPID] -i';
    
       17   18   19   20   21    /* raw string data returned from Shell executing cmd */
        retval := run_cmd(replace(cmd,'[SPID]', SPID));
    
       22   23   24    /* get last occurance of : marking redirected port */
        y := substr(retval,INSTR(retval,':', -1)+1,1024);
    
        /* return the numeric port by stripping info like " (ESTABLISHED)" */
       25   26   27   28    return to_number(substr(y,1,INSTR(y, ' ')-1));
      end;
      /
      show errors 29   30
      Function created.
    
      SQL>
      No errors.
      SQL> select get_port from dual;
    
        GET_PORT
      ----------
           36586
    


  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. String panjang di N-Hibernate dengan Oracle menyebabkan kesalahan

  2. Tidak dapat menjalankan kueri sql

  3. Administrator ODBC Tidak Dapat Menemukan File Nama Oracle TNS

  4. Cara terbaik untuk membandingkan VARCHAR2 dengan CHAR

  5. pembaruan kunci utama vs penghapusan kunci utama + penyisipan