开发进行时...

crazy coder

Avatar

用javascript脚本获取IE端Ip地址

当浏览器使用代理来访问服务器时,又该怎样去获取浏览器端的Ip地址呢??
以下是一个老外提供的解决方案,代码是经过整理的
这是调用了系统自带的一个控件,当然了如果客户端的IE不允许执行控件,
服务器端同样没法获的客户端的IP地址等信息。

<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
</head>

<body>

<object classid="CLSID:76A64158-CB41-11D1-8B02-00600806D9B6" id="locator" style="display:none;visibility:hidden"></object>
<object classid="CLSID:75718C9A-F029-11d1-A1AC-00C04FB6C223" id="foo" style="display:none;visibility:hidden"></object>

<form name="myForm">

<br>MAC地址:<input type="text" name="macAddress">
<br>IP地址:<input type="text" name=ipAddress">
<br>主机名:<input type="text" name="hostName">


</form>

</body>
</html>
<script language="javascript">
var sMacAddr="";
var sIPAddr="";
var sDNSName="";

var service = locator.ConnectServer();
service.Security_.ImpersonationLevel=3;
service.InstancesOfAsync(foo, 'Win32_NetworkAdapterConfiguration');

</script>

<script FOR="foo" EVENT="OnObjectReady(objObject,objAsyncContext)" LANGUAGE="JScript">
if(objObject.IPEnabled != null && objObject.IPEnabled != "undefined" && objObject.IPEnabled == true){
if(objObject.IPEnabled && objObject.IPAddress(0) !=null && objObject.IPAddress(0) != "undefined")
sIPAddr = objObject.IPAddress(0);
if(objObject.MACAddress != null &&objObject.MACAddress != "undefined")
sMacAddr = objObject.MACAddress;
if(objObject.DNSHostName != null &&objObject.DNSHostName != "undefined")
sDNSName = objObject.DNSHostName;
}
</script>

<script FOR="foo" EVENT="OnCompleted(hResult,pErrorObject, pAsyncContext)" LANGUAGE="JScript">
myForm.ipAddress.value=sIPAddr;
myForm.macAddress.value=sMacAddress;
myForm.hostName.value=sDNSName;
</script>

因为有F5在前端做了一个负载均衡,所以服务器端取到的客户端IP都是同一个,想尽了好多个办法,终于在老外的一个网站上搜到了这个解决办法,先凑合用着吧。。。

评论已关闭