--创建存储过程
create procedure getip
@ip varchar(50) output
asdeclare @cmd varchar(100)
set @cmd='ping '+host_name()
create table #(s varchar(200))
insert # exec master..xp_cmdshell @cmd
select @ip=s from # where ltrim(s) like 'Pinging%'
set @ip=substring(@ip,charindex('[',@ip)+1,charindex(']',@ip)-charindex('[',@ip)-1)
drop table #
go--调用
declare @ip varchar(100)
exec p @ip output
select @ip
create procedure getip
@ip varchar(50) output
asdeclare @cmd varchar(100)
set @cmd='ping '+host_name()
create table #(s varchar(200))
insert # exec master..xp_cmdshell @cmd
select @ip=s from # where ltrim(s) like 'Pinging%'
set @ip=substring(@ip,charindex('[',@ip)+1,charindex(']',@ip)-charindex('[',@ip)-1)
drop table #
go--调用
declare @ip varchar(100)
exec p @ip output
select @ip
select hostname from master..sysprocesses where hostname<>''获取IP:--创建存储过程
create procedure getip
@hostname varchar(20),
@ip varchar(50) output
asdeclare @cmd varchar(100)
set @cmd='ping '+@hostname
create table #(s varchar(200))
insert # exec master..xp_cmdshell @cmd
select @ip=s from # where ltrim(s) like 'Pinging%'
set @ip=substring(@ip,charindex('[',@ip)+1,charindex(']',@ip)-charindex('[',@ip)-1)
drop table #
go
/*获得计算机名称和IP地址
/*
摘自:邹建
*/
=======================================================================================
declare @ip varchar(15),@sql varchar(1000)--得到ip地址
create table #ip(a varchar(200))
set @sql='ping '+host_name()+' -a -n 1 -l 1'
insert into #ip exec master..xp_cmdshell @sqlselect @ip=left(a,patindex('%:%',a)-1) from(
select a=substring(a,patindex('Ping statistics for %:%',a)+20,20)
from #ip where a like 'Ping statistics for %:%') a--显示结果
select 用户计算机名=host_name(),ip地址=@ipdrop table #ip
=============================================================================================create table #(txt varchar(200))
insert # exec master..xp_cmdshell 'ipconfig /all'select * from # where ltrim(txt) like 'host name%' or ltrim(txt) like 'ip address%'drop table #--结果
/*
txt
------------------------------------------------------------
Host Name . . . . . . . . . . . . : Lydia IP Address. . . . . . . . . . . . : 192.168.18.188 IP Address. . . . . . . . . . . . : 192.168.18.168 IP Address. . . . . . . . . . . . : 192.168.18.18
(所影响的行数为 4 行)
*/