Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Administration (2000)
 IP Address

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2005-01-04 : 08:41:35
Raed writes "Hi,

How can I know the IP address of each user logged in my server or tried to either through applications, Query Analyzer, Enterprize or any way else.

Thaks,"

SQLServerDBA_Dan
Aged Yak Warrior

752 Posts

Posted - 2005-01-04 : 12:59:07
quote:
Originally posted by AskSQLTeam

Raed writes "Hi,

How can I know the IP address of each user logged in my server or tried to either through applications, Query Analyzer, Enterprize or any way else.

Thaks,"



Here's some code I wrote. Just be sure to remember that this code pings every connection that is connected to the SQL Server. So if you have a lot of connections it will take a while and you may want to talk to you net admin before running it.


-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
/*
ALL SPIDS to IP
2005-01-04: Daniel_Janik@yahoo.com, Dallas, Tx
*/

DECLARE @SPID as int,
@MAXSPID as int,
@MAC as varchar(12),
@MACDisplay as varchar(18),
@IP as varchar(15),
@CMD as varchar(100)

SET @MAXSPID = (SELECT MAX(SPID) FROM master..sysprocesses)
SET @SPID = (SELECT MIN(SPID) FROM master..sysprocesses where status != 'background' and spid > 7)

CREATE TABLE #temp2 (OUTPUT varchar(255))

WHILE @SPID < @MAXSPID
BEGIN
-- SPID to MAC
SELECT @MAC = NET_ADDRESS FROM master..sysprocesses WHERE SPID = @SPID

-- MAC to IP
CREATE TABLE #temp (OUTPUT varchar(255))
SET NOCOUNT ON
INSERT INTO #temp EXEC master..xp_cmdshell 'arp -a'

SELECT @MACDisplay = LEFT(@MAC, 2) + '-' + SUBSTRING(@MAC, 3, 2) + '-' + SUBSTRING(@MAC, 5, 2) + '-' + SUBSTRING(@MAC, 7, 2) + '-' + SUBSTRING(@MAC, 9, 2) + '-' + SUBSTRING(@MAC, 11, 2)
SELECT @IP = SUBSTRING(output, 3, 15) FROM #temp WHERE output LIKE '%' + @MACDisplay + '%'

-- Resolve the IP
SET @CMD = 'master..xp_cmdshell "ping -a ' + @IP + '"'
INSERT INTO #temp2 EXEC (@CMD)

DROP TABLE #temp
SET @SPID = (SELECT MIN(SPID) FROM master..sysprocesses where status != 'background' and spid > @SPID)
END

delete from #temp2 where output not like 'pinging%'
delete from #temp2 where output is null
select distinct * from #temp2
DROP TABLE #temp2
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --


Daniel
SQL Server DBA



** The SPID to MAC to IP was written by someone else. I picked it up about 5 years ago and don't remember who from. I just changed it up a little and added the loop for you.

LOL. It was probably written by NR or Robvolk.
**
Go to Top of Page
   

- Advertisement -