Tuesday 20 January 2015

Get Hostname / Windows Login Name IP Address in SQL Server

Following Code is given to get the name of windows login using IP Address:


declare @IP as varchar(15)
declare @command as varchar(1000) 

set @IP='192.168.1.0' 

SET @command = 'ping -a -n 1' + @IP 

Create Table #Output (Output varchar(150) default(''))

INSERT INTO #Output 
EXEC xp_cmdshell @command
Begin try 
Select top 1 Replace(LEFT([Output],CHARINDEX('[', [output])-2),'Pinging ','') as HostName from #Output where Output is not null 
End Try 
Begin catch 
Select 'Host name for:' + @IP +' could not be find'
End catch 

drop table #Output 


No comments:

Post a Comment