Using sys_context function we can get the hostname and IP address of the current session SQL> select sys_context('userenv','host') from dual; SYS_CONTEXT('USERENV','HOST') -------------------------------------------------------------- WORKGROUP\IBM-L3YMBNP SQL> select sys_context('userenv','ip_address') from dual; SYS_CONTEXT('USERENV','IP_ADDRESS') -----------------------------------------------------------
127.0.0.1 By sys_context we can only get the information of the current session, what if we want to get other sessions? At this time, we use utl_inaddr pack to get it SQL> select utl_inaddr.get_host_address('IBM-L3YMBNP') from dual; UTL_INADDR. GET_HOST_ADDRESS('IBM-L3YMBNP') -------------------------------------------------------------------
9.181.142.152 And we can also get IPs for SINA and other web sites SQL> select utl_inaddr.get_host_address('www.sina.com.cn') from dual; UTL_INADDR. GET_HOST_ADDRESS('WWW. SINA.COM. CN') ---------------------------------------------------------------------
218.30.108.55 The principle is to first obtain the domain name resolution server (resolv.conf), and then determine the resolution order based on the host.conf file, because the default is to prioritize the resolution of the hosts file, and then continue to read the /etc/hosts file. If the hosts file has a parsing relationship, it returns information; If it doesn't exist, then continue to ask the DNS server to get the resolution address, and if it can't, an error will occur SQL> select utl_inaddr.get_host_address('www.a.com') from dual; select utl_inaddr.get_host_address('www.a.com') from dual * ERROR is on line 1:
ORA-29257: 未知的主机 www.a.com
ORA-06512: 在"SYS.UTL_INADDR", line 35
ORA-06512: 在"SYS.UTL_INADDR", line 40
ORA-06512: 在line 1 That's basically it. |