如何通过T-SQL获得当前连接的客户端的IP和机器名
2008-04-27 16:01
666 查看
/*****************************************************************************************************************
下面的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
(如需转载,请注明出处!)
*********************************************************************************************************/
Createprocusp_getClient_infor
as
setnocounton
Declare@rcint
Declare@RowCountint
Select@rc=0
Select@RowCount=0
begin
--//createtemptable,savesp_whoinformation
createtable#tspid(
spidintnull,
ecidintnull,
statusnchar(60)null,
loginnamenchar(256)null,
hostnamenchar(256)null,
blkbitnull,
dbnamenchar(256)null,
cmdnchar(32)
)
--//createtemptablesaveallSQLclientIPandhostnameandlogintime
Createtable#userIP(
[id]intidentity(1,1),
txtvarchar(1000),
)
--//Createresulttabletoreturnrecordset
Createtable#result(
[id]intidentity(1,1),
ClientIPvarchar(1000),
hostnamenchar(256),
login_timedatetimedefault(getdate())
)
--//gethostnamebyexecsp_who,insert#tspidfromsp_who,
insertinto#tspid(spid,ecid,status,loginname,hostname,blk,dbname,cmd)execsp_who
declare@cmdStrvarchar(100),
@hostNamenchar(256),
@userIPvarchar(20),
@sendstrvarchar(100)
--//declareacursorfromtable#tspid
declaretspidcursor
forselectdistincthostnamefrom#tspid with(nolock)wherespid>50
forreadonly
opentspid
fetchnextfromtspidinto@hostname
While@@FETCH_STATUS=0
begin
select@cmdStr='ping'+rtrim(@hostName)
insertinto#userIP(txt)execmaster..xp_cmdshell@cmdStr
select@rowcount=count(id)from#userIP
if@RowCount=2--//noIPfeedbackpackage
begin
insertinto#Result(ClientIP,hostname)values('CannotgetfeedbackpackagefromPing!',@hostname)
end
if@RowCount>2
begin
select@userIP=substring(txt,charindex('[',txt)+1,charindex(']',txt)-charindex('[',txt)-1)
from#userIP
wheretxtlike'Pinging%'
insertinto#Result(ClientIP,hostname)values(@userIP,@hostname)
end
select@rc=@@error
if@rc=0
truncatetable#userIP--//clear#userIPtable
fetchnextfromtspidinto@hostname
end
closetspid
deallocatetspid
select*from#resultwith(nolock)
droptable#tspid
droptable#userIP
droptable#result
end
go
execusp_getClient_infor
下面的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
(如需转载,请注明出处!)
*********************************************************************************************************/
Createprocusp_getClient_infor
as
setnocounton
Declare@rcint
Declare@RowCountint
Select@rc=0
Select@RowCount=0
begin
--//createtemptable,savesp_whoinformation
createtable#tspid(
spidintnull,
ecidintnull,
statusnchar(60)null,
loginnamenchar(256)null,
hostnamenchar(256)null,
blkbitnull,
dbnamenchar(256)null,
cmdnchar(32)
)
--//createtemptablesaveallSQLclientIPandhostnameandlogintime
Createtable#userIP(
[id]intidentity(1,1),
txtvarchar(1000),
)
--//Createresulttabletoreturnrecordset
Createtable#result(
[id]intidentity(1,1),
ClientIPvarchar(1000),
hostnamenchar(256),
login_timedatetimedefault(getdate())
)
--//gethostnamebyexecsp_who,insert#tspidfromsp_who,
insertinto#tspid(spid,ecid,status,loginname,hostname,blk,dbname,cmd)execsp_who
declare@cmdStrvarchar(100),
@hostNamenchar(256),
@userIPvarchar(20),
@sendstrvarchar(100)
--//declareacursorfromtable#tspid
declaretspidcursor
forselectdistincthostnamefrom#tspid with(nolock)wherespid>50
forreadonly
opentspid
fetchnextfromtspidinto@hostname
While@@FETCH_STATUS=0
begin
select@cmdStr='ping'+rtrim(@hostName)
insertinto#userIP(txt)execmaster..xp_cmdshell@cmdStr
select@rowcount=count(id)from#userIP
if@RowCount=2--//noIPfeedbackpackage
begin
insertinto#Result(ClientIP,hostname)values('CannotgetfeedbackpackagefromPing!',@hostname)
end
if@RowCount>2
begin
select@userIP=substring(txt,charindex('[',txt)+1,charindex(']',txt)-charindex('[',txt)-1)
from#userIP
wheretxtlike'Pinging%'
insertinto#Result(ClientIP,hostname)values(@userIP,@hostname)
end
select@rc=@@error
if@rc=0
truncatetable#userIP--//clear#userIPtable
fetchnextfromtspidinto@hostname
end
closetspid
deallocatetspid
select*from#resultwith(nolock)
droptable#tspid
droptable#userIP
droptable#result
end
go
execusp_getClient_infor
相关文章推荐
- 如何通过T-SQL获得当前连接的客户端的IP和机器名...
- 如何通过T-SQL获得当前连接的客户端的IP和机器名...
- SQL获得当前连接客户端IP和机器名
- 如何通过配置F5 使业务程序获得真实客户端IP 及 IIS 日志记录真实客户端的IP
- 通过新浪IP服务器获得的当前客户端IP地址对应的国家、省份或直辖市、城市信息
- 如何通过在 SQL Server 的早期版本使用客户端工具连接到的 SQL Server 2005 或 SQL Server 2000 命名实例
- 如何通过腾讯SOSO问问获得巨大IP流量?
- 如何获取Android手机连接当前网络的外网IP
- 如何通过.Net Compact Framework来获得应用程序的当前路径
- 如何能获得自己机器的外部IP呀
- com.microsoft.sqlserver.jdbc.SQLServerException: 通过端口 1443 连接到主机 localhost 的 TCP/IP 连接失败。
- MSTR如何通过实体实现SQL中的连接查询
- 获得当前页面客户端的IP
- pl/sql 如何配置连接远程一个或多个数据库(1.同一个ip不同用户名,2.不同ip不同用户名)以及记住密码
- 数据表中有字段time(类型为varchar),如何通过sql语句找出这个字段里力离当前最近的一个日期
- 关于socket通信的理解,socket连接通过IP+端口号的方式,那么如果两个服务器端的应用在同一台机器上,并且位于同一个tomcat容器下,那么岂不是两个服务器都收到了客户端发送的信息
- 如何通过抓包查看客户端https连接中ssl/tls加密所采用的秘钥位数
- 如何获取Android手机连接网络的当前局域网IP
- 如何配置pl/sql (本地客户端)连接远程oracle服务器
- 如何ibatis通过id获得sql?