Get IP Address of Host using T-SQL

DECLARE @ip varchar(40)
DECLARE @ipLine varchar(200)
DECLARE @pos int

SET nocount on
SET @ip = NULL

Create table #temp (ipLine varchar(200))
INSERT #temp EXEC master..xp_cmdshell 'ipconfig'

SELECT top 1 @ipLine = ipLine FROM #temp
WHERE upper (ipLine) like '%IPV4 ADDRESS%' or upper (ipLine) like '%IP ADDRESS%'
    IF (isnull (@ipLine,'***') != '***')
      BEGIN 
       SET @pos = CharIndex (':',@ipLine,1);
       SET @ip = rtrim(ltrim(substring (@ipLine , @pos + 1 ,len (@ipLine) - @pos)))
      END 
PRINT @ip
DROP TABLE #temp
SET nocount off


Advertisements