SESSION SID:
SELECT sys_context('USERENV', 'SID') FROM DUAL;
ORACLE_SID:
SELECT sys_context('userenv','instance_name') FROM dual;
HOST (databasemaskine) :
SELECT UTL_INADDR.get_host_name FROM dual;
Adgangskode:Se http://www.dba-oracle.com/t_password_storage.htm
CLIENT SIDE Port:
Select port from v$session;
SERVER OS PID for Oracle-serverprocessen forbundet til klientprocessen
SELECT p.spid
FROM v$process p, v$session s
WHERE s.paddr = p.addr and
sys_context('USERENV', 'SID') = s.sid;
Port på serversiden:
Dette er virkelig svært, da serverprocesporten er kortlagt til en anden port end den oprindelige - for eksempel port 12102 i listener.ora/tnsnames.ora bliver kortlagt af lytteren til en vilkårlig gratis port (forresten:det er grunden til, at ofte firewalls skal lukkes ned, og mens forbindelser ikke kan komme efter kortlægning gennem firewall. Dette kan rettes, men dette er en anden historie)
Nedenfor kode
-
giver de nødvendige privilegier
-
definerer en Java-lagret funktion "Util.RunThis", som udfører en OS-kommando på databaseserveren, der er sendt til funktion og returstreng.
-
"Util.RunThis" er knyttet til PL/SQL-funktionen "RUN_CMD"
-
"GET_PORT" er en PL/SQL-funktion, der returnerer en numerisk værdi af den brugte port på databaseserverprocessen, der er forbundet med klientsessionen. Inde i GET_PORT bruges SELECT til at bestemme databaseserverens proces-pid, der erstatter [SPID] i kommandoen nedenfor
/usr/sbin/lsof -Pan -p [SPID] -i
-
endelig ved at påberåbe os et simpelt valg får vi Port of database server-processen knyttet til den aktuelle session
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