SESSION SID:
SELECT sys_context('USERENV', 'SID') FROM DUAL;
ORACLE_SID:
SELECT sys_context('userenv','instance_name') FROM dual;
HOST (databasmaskin) :
SELECT UTL_INADDR.get_host_name FROM dual;
Lösenord:Se http://www.dba-oracle.com/t_password_storage.htm
CLIENT SIDE Port:
Select port from v$session;
SERVER OS PID för Oracle-serverprocessen ansluten till 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å serversidan:
Detta är verkligen svårt eftersom serverprocessporten är mappad till en annan port än den initiala - till exempel port 12102 i listener.ora/tnsnames.ora mappas av lyssnaren till en godtycklig gratis (förresten:det är anledningen till att ofta brandväggar måste stängas av och medan anslutningar inte kan fås efter kartläggning via brandvägg. Detta kan fixas men det här är en annan historia)
Nedan kod
-
ger nödvändiga privilegier
-
definierar en Java-lagrad funktion "Util.RunThis" som kör ett OS-kommando på databasservern som skickas till funktion och retursträng.
-
"Util.RunThis" är mappad till PL/SQL-funktionen "RUN_CMD"
-
"GET_PORT" är en PL/SQL-funktion som returnerar ett numeriskt värde för den använda porten för databasserverprocessen kopplad till klientsessionen. Inuti GET_PORT används SELECT för att fastställa databasserverns process-pid som ersätter [SPID] i kommandot nedan
/usr/sbin/lsof -Pan -p [SPID] -i
-
Genom att slutligen anropa ett enkelt val får vi Port of databasserverprocessen kopplad till aktuell 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