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

One Response to Get IP Address of Host using T-SQL

  1. vittop says:

    Great post, thank you !

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: