Get Host Name in Oracle & SYS_CONTEXT('USERENV', xxx)
Oracle提供了好些方法来获取连接数据库的客户端机器名和数据库所在的机器名,最常见的是从视图v$instance和v$session中获得,如下....
V$INSTANCE
v$instance中的host_name显示的是数据库服务器所在的机器名...
SQL> select host_name from v$instance;
HOST_NAME
----------------------------------------------------------------
SZV-DEV-LO-D02
SQL>
V$SESSION
v$session中的MACHINE (TERMINAL)显示的客户端session所在的机器名。
注意到MACHINE会包含域名信息。
SQL> select terminal, machine from v$session where sid= sys_context('userenv', 'sid');
TERMINAL MACHINE
---------------- ------------------------------
SZ1-DEV-DXP-047 ANALYTICS\SZ1-DEV-DXP-047
除了这两种方式,还可以通过调用UTL_INADDR包中提供的一些function来得到数据库服务器端的主机名
UTL_INADDR
- GET_HOST_NAME: 机器名
- GET_HOST_ADDRESS:IP地址
SQL> select utl_inaddr.get_host_address, utl_inaddr.get_host_name from dual;
GET_HOST_ADDRESS GET_HOST_NAME
-------------------- --------------------
10.133.131.154 SZV-DEV-LO-D02
上面提到的方法返回的信息都不是很全面,得到的要么只是客户端session所在的机器名,要么是数据库服务器的机器名。其实这些信息都可以通过SYS_CONTEXT函数得到。
SYS_CONTEXT
SYS_CONTEXT的USERENV命名空间中包含了当前SESSION相关的很多信息,其中就包括了Host Name, 相关的list 如下...
Predefined Parameters of Namespace USERENV
Parameter | Return Value |
---|---|
|
Identifies the position in the module (application name) and is set through the |
|
Returns the cursor ID of the SQL that triggered the audit. This parameter is not valid in a fine-grained auditing environment. If you specify it in such an environment, Oracle Database always returns |
|
Returns the identity used in authentication. In the list that follows, the type of user is followed by the value returned:
|
|
Data being used to authenticate the login user. For X.503 certificate authenticated sessions, this field returns the context of the certificate in HEX2 format. Note: You can change the return value of the |
|
Returns the method of authentication. In the list that follows, the type of user is followed by the method returned:
You can use |
|
Job ID of the current session if it was established by an Oracle Database background process. Null if the session was not established by a background process. |
|
Returns an identifier that is set by the application through the |
|
Returns up to 64 bytes of user session information that can be stored by an application using the |
|
The bind variables for fine-grained auditing. |
|
Name of the default schema being used in the current schema. This value can be changed during the session with an |
|
Identifier of the default schema being used in the current session. |
|
|
|
The length of the current SQL statement that triggers fine-grained audit or row-level security (RLS) policy functions or event handlers. Valid only inside the function or event handler. |
|
Domain of the database as specified in the |
|
Name of the database as specified in the |
|
Name of the database as specified in the |
|
The current audit entry number. The audit entryid sequence is shared between fine-grained audit records and regular audit records. You cannot use this attribute in distributed SQL statements. The correct auditing entry identifier can be seen only through an audit handler for standard or fine-grained audit. |
|
Returns the user's enterprise-wide identity:
The value of the attribute differs by proxy method:
|
|
Job ID of the current session if it was established by a client foreground process. Null if the session was not established by a foreground process. |
|
Returns the number being used in the System Global Area by the globally accessed context. |
|
Returns the global user ID from Oracle Internet Directory for Enterprise User Security (EUS) logins; returns null for all other logins. |
|
Name of the host machine from which the client has connected. |
|
Returns the way the user's schema was created in the database. Specifically, it reflects the
|
|
The instance identification number of the current instance. |
|
The name of the instance. |
|
IP address of the machine from which the client is connected. |
|
Returns |
|
The ISO abbreviation for the language name, a shorter form than the existing ' |
|
The language and territory currently used by your session, along with the database character set, in this form: language_territory.characterset |
|
The application name (module) set through the |
|
Network protocol being used for communication, as specified in the ' |
|
The current calendar of the current session. |
|
The currency of the current session. |
|
The date format for the session. |
|
The language used for expressing dates. |
|
|
|
The territory of the current session. |
|
Operating system user name of the client process that initiated the database session. |
|
The invoker of row-level security (RLS) policy functions. |
|
Returns the Oracle Internet Directory DN when the proxy user is an enterprise user. |
|
Returns the global user ID from Oracle Internet Directory for Enterprise User Security (EUS) proxy users; returns |
|
Name of the database user who opened the current session on behalf of |
|
Identifier of the database user who opened the current session on behalf of |
|
The host name of the machine on which the instance is running. |
|
The name of the service to which a given session is connected. |
|
For enterprises users, returns the schema. For other users, returns the database user name by which the current user is authenticated. This value remains the same throughout the duration of the session. |
|
Identifier of the database user name by which the current user is authenticated. |
|
The auditing session identifier. You cannot use this attribute in distributed SQL statements. |
|
The session number (different from the session ID). |
|
The auditing statement identifier. |
|
The operating system identifier for the client of the current session. In distributed SQL statements, this attribute returns the identifier for your local session. In a distributed environment, this is supported only for remote |
select sys_context('userenv', 'host') client_host,
sys_context('userenv', 'terminal') termial,
sys_context('userenv', 'server_host') server_host,
sys_context('userenv', 'ip_address') client_ip
from dual;
CLIENT_HOST TERMINAL SERVER_HOST CLIENT_IP
------------------------------ -------------------- -------------------- -----------------
ANALYTICS\SZ1-DEV-DXP-047 SZ1-DEV-DXP-047 szv-dev-lo-d02 10.5.130.100
--------------------------------------
Regards,
FangwenYu
![](https://pic.cnblogs.com/avatar/93867/20191016181427.png)