监控目前所有连接SQL SERVER的用户信息

本文涉及的产品
云数据库 RDS SQL Server,独享型 2核4GB
简介: 原文:监控目前所有连接SQL SERVER的用户信息 if object_id('p_getlinkinfo','P')is not null drop proc p_getlinkinfo go create proc p_getlinkinfo @dbname sysname=nul...
原文: 监控目前所有连接SQL SERVER的用户信息

if object_id('p_getlinkinfo','P')is not null drop proc p_getlinkinfo
go
create proc p_getlinkinfo 
@dbname sysname=null, --要查詢的數據庫名,默認表示所有 
@includeip bit=0 --是否顯示IP信息 
as 
begin
declare @dbid int 
set @dbid=db_id(@dbname)
if object_id('tempdb..#tb')is not null drop table #tb
if object_id('tempdb..#ip')is not null drop table #ip 
create table #tb
(id int identity(1,1),
dbname sysname,
hostname nchar(128),
loginname nchar(128),
net_address nchar(12),
net_ip nvarchar(15),
prog_name nchar(128)) 
insert into #tb(hostname,dbname,net_address,loginname,prog_name) 
select distinct hostname,
db_name(dbid),
net_address,
loginame,
program_name
from master..sysprocesses 
where hostname!=''and(@dbid is null or dbid=@dbid) 
if @includeip=0 goto lb_show --不顯示IP 
declare @sql varchar(500),@hostname nchar(128),@id int 
create table #ip(hostname nchar(128),a varchar(200)) 
declare tb cursor local for select distinct hostname from #tb 
open tb 
fetch next from tb into @hostname 
while @@fetch_status=0 
begin 
set @sql='ping '+@hostname+' -a -n 1 -l 1' 
insert #ip(a) exec master..xp_cmdshell @sql 
update #ip set hostname=@hostname where hostname is null 
fetch next from tb into @hostname 
end 
update #tb set net_ip=left(a,patindex('%:%',a)-1) 
from #tb a inner join 
(select hostname,a=substring(a,patindex('Ping statistics for %:%',a)+20,20) 
from #ip 
where a like'Ping statistics for %:%')b
on a.hostname=b.hostname 
lb_show: 
select id,
dbname,
hostname,
loginname,
net_address,
net_ip,
prog_name 
from #tb 
end 
go 
exec p_getlinkinfo @dbname='master',@includeip=1

  消息15281,级别16,状态1,过程xp_cmdshell,第1 行
  SQL Server 阻止了对组件'xp_cmdshell' 的过程'sys.xp_cmdshell' 的访问,因为此组件已作为此服务器安全配置的一部分而被关闭。系统管理员可以通过使用sp_confi  gure 启用'xp_cmdshell'。有关启用'xp_cmdshell' 的详细信息,请参阅SQL Server 联机丛书中的"外围应用配置器"。

SELECT * FROM sys.configurations ORDER BY name ; sp_configure 'show advanced options', 1; go reconfigure; go sp_configure 'xp_cmdshell', 1; go reconfigure; go

 

相关实践学习
使用SQL语句管理索引
本次实验主要介绍如何在RDS-SQLServer数据库中,使用SQL语句管理索引。
SQL Server on Linux入门教程
SQL Server数据库一直只提供Windows下的版本。2016年微软宣布推出可运行在Linux系统下的SQL Server数据库,该版本目前还是早期预览版本。本课程主要介绍SQLServer On Linux的基本知识。 相关的阿里云产品:云数据库RDS SQL Server版 RDS SQL Server不仅拥有高可用架构和任意时间点的数据恢复功能,强力支撑各种企业应用,同时也包含了微软的License费用,减少额外支出。 了解产品详情: https://www.aliyun.com/product/rds/sqlserver
目录
相关文章
|
25天前
|
SQL 安全 算法
【SQL server】玩转SQL server数据库:第四章 数据库安全性
【SQL server】玩转SQL server数据库:第四章 数据库安全性
65 12
|
25天前
|
SQL 人工智能 算法
【SQL server】玩转SQL server数据库:第二章 关系数据库
【SQL server】玩转SQL server数据库:第二章 关系数据库
63 10
|
25天前
|
SQL 存储 算法
【SQL server】玩转SQL server数据库:第一章 绪论
【SQL server】玩转SQL server数据库:第一章 绪论
42 5
|
25天前
|
SQL 数据库 数据库管理
【SQL server】玩转SQL server数据库:第三章 关系数据库标准语言SQL(一)模式、表、索引与视图
【SQL server】玩转SQL server数据库:第三章 关系数据库标准语言SQL(一)模式、表、索引与视图
58 11
|
8天前
|
SQL 数据可视化 算法
SQL Server聚类数据挖掘信用卡客户可视化分析
SQL Server聚类数据挖掘信用卡客户可视化分析
|
25天前
|
SQL 算法 数据库
【SQL server】玩转SQL server数据库:第三章 关系数据库标准语言SQL(二)数据查询
【SQL server】玩转SQL server数据库:第三章 关系数据库标准语言SQL(二)数据查询
98 6
|
6天前
|
SQL 机器学习/深度学习 数据采集
数据分享|SQL Server、Visual Studio、tableau对信贷风险数据ETL分析、数据立方体构建可视化
数据分享|SQL Server、Visual Studio、tableau对信贷风险数据ETL分析、数据立方体构建可视化
|
7天前
|
SQL 数据采集 数据挖掘
SQL Server仓储物流公司visual studio发货数据仓库设计
SQL Server仓储物流公司visual studio发货数据仓库设计