如何从连接中获取客户端ip地址?当然包括获取应用程序打开的连接!使用exec sp_who 'active'看到的之是计算机名

解决方案 »

  1.   

    http://www.xfly.com.cn/Article/ITExam/Macrmedia/200606/Article_17252.html
      

  2.   

    看了一下代码,用exec master..cmd_shell 'ping computername'
    再截取返回的字符窜中的ip部分
      

  3.   

    我这有段完整的代码,贴出来create table #ip(id int identity(1,1),re varchar(200))declare @s varchar(1000)
    set @s='ping '+host_name()+' -a -n 1 -l 1'
    insert #ip(re) exec master..xp_cmdshell @sselect 登录的用户=suser_sname(),计算机名=host_name(),IP地址=stuff(left(re,charindex(']',re)-1),1,charindex('[',re),'')
    from #ip
    where id=2drop table #ip
      

  4.   


    /*--获取连接SQL服务器的信息所有连接本机的:操作的数据库名,计算机名,用户名,网卡物理地址,IP地址,程序名--邹建 2003.11(引用请保留此信息)--*//*--调用示例
    --显示所有本机的连接信息
    exec p_getlinkinfo--显示所有本机的连接信息,包含ip地址
    exec p_getlinkinfo @includeip=1--显示连接指定数据库的信息
    exec p_getlinkinfo '客户资料'
    --*/
    create proc p_getlinkinfo
    @dbname sysname=null,--要查询的数据库名,默认查询所有数据库的连接信息
    @includeip bit=0--是否显示IP地址,因为查询IP地址比较费时,所以增加此控制
    as
    declare @dbid int
    set @dbid=db_id(@dbname)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
    endupdate #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.hostnamelb_show:
    select id,数据库名=dbname,客户机名=hostname,用户名=loginname
    ,网卡物理地址=net_address,IP地址=net_ip,应用程序名称=prog_name from #tb
    GO
      

  5.   

    set @sql='ping '+@hostname+' -a -n 1 -l 1'对adsl动态ip的恐怕不适用吧
      

  6.   

    我是想知道如果是通过网页脚本登录了sqlserver,不使用如asp,jsp,php等脚本来记录对方的ip,单单根据从数据库服务器的连接进程来获取ip行不?
    因为ping程序对像一般的浏览网页的用户来说,ping他的机器名你是ping不通的,所以就不可以获取对方的ip了。