转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 数据库 >> Sql Server >> 正文
如何通过T-SQL获得当前连接的客户端的IP和机器名         

如何通过T-SQL获得当前连接的客户端的IP和机器名

作者:闵涛 文章来源:闵涛的学习笔记 点击数:1101 更新时间:2007/11/14 11:00:47

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

    下面的SP是返回所有的客户端的IP和HOSTNAME,目的是可以通过JOB返回某一时间点的CLIENT 的连接情况.

     我当时写这个脚本的目的是经常有一些没有授权的客户机,通过SQLSERVER的CLIENT就连接到SQLSERVER,所以我可以定义一个JOB每隔30分钟运行一次这个存储过程,并且将内容写的一个LOG文件,这样可以大概记录有哪些CLIENT在连接SQLSERVER,当然大家可以可以修改这个脚本,使之返回更多的信息,比如CPU,MEMORY,LOCK....

Author:黄山光明顶

mail:leimin@jxfw.com

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 @rc=@@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


[电脑技术]电脑主机上使用电视卡如何通过电视精灵搜索电视频…  [聊天工具]如何通过http代理上QQ?
[Delphi程序]如何通过COM接口得到实现该接口的对象实例  [ORACLE]如何通过struts以oci协议将文件上传到oracle数据库…
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · Sql Server  · MySql
    · Access  · ORACLE
    · SyBase  · 其他
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台