Tuesday, April 1, 2008

Get IP Address from hostname within Oracle Database

We everyone know that with ping command we can get IP Address of another computer.
Suppose from terminal on Solaris machine,use
ping -s hostname to get IP Address of the host.

From Terminal of Linux Machine, use
ping hostname to get IP Address of the host.

But within oracle database how we can convert hostname to IP address?

It can be done by UTL_INADDR package. Within this package GET_HOST_ADDRESS subprograms takes a varchar2 datatype within which we can give the host name for which we want to get IP.

For example, to get IP address of computer saturn, use


SQL> select UTL_INADDR.GET_HOST_ADDRESS('saturn') from dual;


UTL_INADDR.GET_HOST_ADDRESS('SATURN')
--------------------------------------------------------------------------------
192.168.1.11


We can easily use it to see which IP address is now connected to oracle database. Like,
COL machine format a30
COL ip format a20

select sid, machine,
UTL_INADDR.get_host_address (substr(machine,instr(machine,'\')+1)) ip
from v$session where type='USER' and username is not null
order by sid;


or,

select sid,machine,UTL_INADDR.GET_HOST_ADDRESS (machine)
from v$session
where type = 'USER' and username is not null
order by sid;


While issuing this query you may get error ORA-29257, ORA-06512 like

ERROR:
ORA-29257: host appdemo unknown
ORA-06512: at "SYS.UTL_INADDR", line 19
ORA-06512: at "SYS.UTL_INADDR", line 40
ORA-06512: at line 1

For which host you get just exclude that in predicate. Like,

SQL> select sid, machine,
UTL_INADDR.get_host_address (substr(machine,instr(machine,'\')+1)) ip
from v$session where type='USER' and username is not null and machine not in ('quark.arjubd.com', 'nilanjona','sinewave','loris.arjubd.com','appdemo','nilanjona.arjubd.com');

Related Documents
How to find the User who is connected to Oracle
How to find current session ID
How to know which objects are being accessed by a user

10 comments:

itsmyscrapyard said...

simply genious

d_d_f said...

Yes, it's a wonderful package provided Java is installed in the database, otherwise anyone trying to use this function will receive the following error:

ERROR at line 1:
ORA-29540: class oracle/plsql/net/InternetAddress does not exist
ORA-06512: at "SYS.UTL_INADDR", line 21
ORA-06512: at "SYS.UTL_INADDR", line 33
ORA-06512: at line 1

Funny how you didn't mention that requirement when you 'discussed' this package.

Mirza's said...

Salamz Bro,

Same error here, Plz help

ORA-29257: host GIL-DBA unknown
ORA-06512: at "SYS.UTL_INADDR", line 19
ORA-06512: at "SYS.UTL_INADDR", line 40
ORA-06512: at line 1


mirza2k8@gmail.com

Abul said...

Hi how to get Machine name from IP Address in oracle.

Abul said...

is it possible to get Hostname from ip Address in oracle?...

-
Regards
itabul@gmail.com

Arju said...

Machine name is not different from hostname. In the post it is discussed elaborately.

Abul said...

Guys i got it,

SELECT utl_inaddr.get_host_address('google.com')
FROM dual;

&

SELECT utl_inaddr.get_host_name('74.125.45.100')
FROM dual;

Thank you....
Regards.
Abul

Arju said...

Also, you are welcome to post all of your problems inside http://arju-on-it.com

Mirza's said...

ORA-29257: host 220.227.71.44 unknown
ORA-06512: at "SYS.UTL_INADDR", line 4
ORA-06512: at "SYS.UTL_INADDR", line 35
ORA-06512: at line 1

Arju said...

If you are on windows, just do a simple test,
>ping -a 4.2.2.2

If you can't get hostname you can't expect to get hostname from oracle