After Running this query one trigger and one table will be created.
Trigger Name : block_ipaddress
Table Name: IPBLock
The add ip addresses which are needed to block.
********************************************************************************
USE [master]
GO
/****** Object: Table [dbo].[IPBLock] Script Date: 04/20/2017 13:43:41 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[IPBLock](
[ipaddress] [varchar](15) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object: DdlTrigger [block_ipaddress] Script Date: 04/19/2017 20:24:57 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TRIGGER [block_ipaddress]
ON ALL SERVER
FOR LOGON
AS
BEGIN
DECLARE @capturedip NVARCHAR(15);
SET @capturedip = (SELECT EVENTDATA().value('(/EVENT_INSTANCE/ClientHost)[1]', 'NVARCHAR(15)'));
IF EXISTS(SELECT ipaddress FROM master.dbo.IPBLock WHERE ipaddress = @capturedip)
BEGIN
Print 'Your IP Address is blocked, Contact Administrator'
ROLLBACK
END
ELSE
BEGIN
DECLARE @IPRange VARCHAR(15)
SELECT @IPRange= SUBSTRING(@capturedip,1,LEN(@capturedip)-CHARINDEX('.',REVERSE(@capturedip)))+'.*'
IF EXISTS(SELECT ipaddress FROM master.dbo.IPBLock WHERE ipaddress = @IPRange)
BEGIN
Print 'Your IP Address Range is blocked, Contact Administrator'
ROLLBACK
END
END
END
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
ENABLE TRIGGER [block_ipaddress] ON ALL SERVER
GO
*********************************************************************************
No comments:
Post a Comment