sqlplus / as sysdba create tablespace test datafile;
通过ctas从v$session创建session历史记录表指定表空间test
sqlplus / as sysdba create table session_history tablespace test as (select sid,username,program,machine,'000.000.000.000'ipadd,sysdate moditime from v$session where 0=1);
创建触发器,当有用户登录时,将记录插入session历史记录表
sqlplus / as sysdba CREATE or replace trigger on_logon_trigger after logon ON database begin INSERT INTO session_history SELECT sid,username,program,machine,sys_context('userenv','ip_address'),sysdate FROM v$session WHERE audsid = userenv('sessionid'); end; /
本机通过lucifer用户登录
sqlplus lucifer/lucifer@10.211.55.110/orcl
查询非SYS用户的登录记录
sqlplus / as sysdba alter session set nls_date_format ='yyyy-mm-dd hh24:mi:ss'; select * from session_history q where q.username not in('SYS');
sqlplus / as sysdba drop trigger on_logon_trigger; drop tablespace test;
测试是否还能看到IP
sqlplus lucifer/lucifer@10.211.55.110/orcl sqlplus / as sysdba alter session set nls_date_format ='yyyy-mm-dd hh24:mi:ss'; select username,machine,terminal,program,client_info,logon_time from v$session;
sqlplus / as sysdba exec DBMS_SESSION.set_identifier(SYS_CONTEXT('USERENV', 'IP_ADDRESS')); /
主机测试用户登录是否能查看IP地址
sqlplus lucifer/lucifer@10.211.55.110/orcl alter session set nls_date_format ='yyyy-mm-dd hh24:mi:ss'; select sys_context('userenv','ip_address') from dual;
sqlplus / as sysdba create or replace trigger on_logon_triggerafter logon on database begindbms_application_info.set_client_info(sys_context('userenv','ip_address')); end; /
查询v$session查看是否有记录IP地址
sqlplus / as sysdba select username,machine,terminal,program,client_info,logon_time from v$session where username is not null;