审计sqlserver ip和主机名

declare @ip varchar(200)
select @ip=''
declare @num int
select @num=1
while(@num=1)
begin
SELECT @ip=b.[client_net_address]
FROM MASTER.sys.dm_exec_sessions a 
INNER JOIN MASTER.sys.dm_exec_connections b 
ON a.session_id=b.session_id 
and a.[host_name]='0.0.0.0'
--and a.[host_name]='book3'
if @@rowcount>0
begin
print @ip
select @num=0
end

end


select b.[session_id],b.[login_time],b.[host_name],b.[original_login_name],a.[client_net_address],a.TEXT 
from  MASTER.sys.dm_exec_sessions b,
(
SELECT SESSION_ID,[client_net_address],TEXT
FROM
 SYS.DM_EXEC_CONNECTIONS
CROSS APPLY
 SYS.DM_EXEC_SQL_TEXT(MOST_RECENT_SQL_HANDLE)) a where a.SESSION_ID=b.SESSION_ID and a.TEXT like 'select%'


----------------------------------------------------------------------------------------------------------------------------

创建IP和主机名对应关系表

USE [xxxxxxx]
GO

CREATE TABLE [dbo].[audit_host](
[host_name] [nvarchar](128) NULL,
[client_net_address] [varchar](48) NULL
) ON [PRIMARY]

GO



一秒钟执行一次新连接插入audit_host表

declare @num int
select @num=1
while(@num=1)
begin
insert into audit_host
select * from (SELECT 
distinct a.[host_name],b.[client_net_address] 
FROM MASTER.sys.dm_exec_sessions a WITH(NOLOCK)
INNER JOIN MASTER.sys.dm_exec_connections b WITH(NOLOCK)
ON a.session_id=b.session_id) c where not exists (select 1 from audit_host d where c.[host_name]=d.[host_name] and c.[client_net_address]=d.[client_net_address])
WAITFOR DELAY '00:00:01'
end



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值