国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

如何通過T-SQL獲得當前連接的客戶端的IP和機器名

2024-07-21 02:08:23
字體:
來源:轉載
供稿:網友

/*****************************************************************************************************************

    下面的sp是返回所有的客戶端的ip和hostname,目的是可以通過job返回某一時間點的client 的連接情況.

     我當時寫這個腳本的目的是經常有一些沒有授權的客戶機,通過sqlserver的client就連接到sqlserver,所以我可以定義一個job每隔30分鐘運行一次這個存儲過程,并且將內容寫的一個log文件,這樣可以大概記錄有哪些client在連接sqlserver,當然大家可以可以修改這個腳本,使之返回更多的信息,比如cpu,memory,lock....

author:黃山光明頂

mail:[email protected]

version:1.0.0

date:2004-1-30

(如需轉載,請注明出處!)

*********************************************************************************************************/

create proc usp_getclient_infor
as
set nocount on

declare @rc int
declare @rowcount int

select @rc=0
select @rowcount=0

begin
--//create temp table ,save sp_who information
  create table #tspid(
 spid int null,
 ecid int null,
 status nchar(60) null,
 loginname nchar(256) null,
 hostname nchar(256) null,
 blk bit null,
 dbname nchar(256) null,
 cmd nchar(32)
  )

--//create temp table save all sql client ip and hostname and login time
create table #userip(
 [id]int identity(1,1),
 txt varchar(1000),
)

--//create result table to return recordset
create table #result(
 [id]int identity(1,1),
 clientip varchar(1000),
 hostname nchar(256),
 login_time datetime default(getdate())

 )
--//get host name by exec sp_who ,insert #tspid from sp_who,
insert into #tspid(spid,ecid,status,loginname,hostname,blk,dbname,cmd) exec sp_who

declare @cmdstr varchar(100),
 @hostname nchar(256),
 @userip varchar(20),
 @sendstr varchar(100)
 

--//declare a cursor from table #tspid
declare tspid cursor
for select distinct hostname from #tspid  with (nolock) where spid>50
for read only

open tspid
   fetch next from tspid into @hostname
   while @@fetch_status = 0
   begin
 select @cmdstr='ping '+rtrim(@hostname)
 
 insert into #userip(txt) exec master..xp_cmdshell @cmdstr
 
 select @rowcount=count(id) from #userip


 if @rowcount=2 --//no ip feedback package
  begin
  insert into #result(clientip,hostname) values('can not get feedback package from ping!',@hostname)
  end
 if @rowcount>2 
  begin
  select @userip=substring(txt,charindex('[',txt)+1,charindex(']',txt)-charindex('[',txt)-1)
  from #userip
  where txt like 'pinging%'
  
  insert into #result(clientip,hostname) values(@userip,@hostname)
  end
 select @[email protected]@error
 if @rc=0
  truncate table #userip --//clear #userip table

   fetch next from tspid into @hostname
 end

close tspid
deallocate tspid

select * from #result with(nolock)

drop table #tspid
drop table #userip
drop table #result
end
go
exec usp_getclient_infor
國內最大的酷站演示中心!
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 马尔康县| 城固县| 鞍山市| 陇南市| 二连浩特市| 湖南省| 略阳县| 枣强县| 台湾省| 射洪县| 柯坪县| 阿坝| 藁城市| 苏尼特右旗| 邹平县| 德江县| 卫辉市| 潜江市| 乌拉特后旗| 罗甸县| 贵阳市| 新河县| 内江市| 丰原市| 怀安县| 天津市| 蒙自县| 秦安县| 利辛县| 罗田县| 甘肃省| 额敏县| 阳朔县| 普安县| 璧山县| 阜新市| 定远县| 于都县| 天峻县| 普兰县| 堆龙德庆县|