User Tools

Site Tools


dotnet:mssqlreversedns

MSSQL Reverse DNS

-- To allow advanced options to be changed.
EXEC sp_configure 'show advanced options', 1;
GO
-- To update the currently configured value for advanced options.
RECONFIGURE;
GO
-- To enable the feature.
EXEC sp_configure 'xp_cmdshell', 1;
GO
-- To update the currently configured value for this feature.
RECONFIGURE;
GO
DECLARE @ip_address VARCHAR(16)
DECLARE @cmd VARCHAR(100)
DECLARE @host_name VARCHAR(100)
 
DECLARE curs cursor LOCAL READ_ONLY
--for select ip_address from ip_address_data where arp_domain is null
FOR 
SELECT DISTINCT AwLog.Host
FROM AwLog
	LEFT JOIN DNS ON DNS.Ip = AwLog.Host	
WHERE DNS.Ip IS NULL	
OPEN curs
 
CREATE TABLE #output(line nvarchar(100))
 
while 1=1
BEGIN
fetch NEXT FROM curs INTO @ip_address
IF @@fetch_status != 0 break
 
SET @cmd = 'nslookup '+@ip_address
 
INSERT INTO #output
EXECUTE xp_cmdshell @cmd
 
--update ip_address_data set
--arp_domain = (select substring(line,10,99) from #output where line like 'Name%'),
--last_cached = getdate()
--where ip_address = @ip_address
SELECT @host_name = SUBSTRING(line,10,99) FROM #output WHERE line LIKE 'Name%'
print(@host_name)
INSERT INTO DNS (Ip, HostName) VALUES (@ip_address, @host_name)
 
DELETE #output
 
END
 
DROP TABLE #output
close curs
dotnet/mssqlreversedns.txt · Last modified: 2019/04/27 16:45 (external edit)