今天在搞JS(javascript)獲取客戶端IP的小程序,上網搜了下,好多在如今的系統和瀏覽器中的都無效,很無奈,在Chrome、FireFox中不多搞到直接利用ActiveX獲取IP等的JS腳本。下面的代碼是我在全部windowsNT5.0及以上的系統上都測試經過的,給出代碼:
javascript
獲取客戶端IP。
html
[html] view plaincopyjava
<HTML> 小程序
<HEAD> windows
<TITLE>GetLocalIP</TITLE> 瀏覽器
</HEAD> 網絡
<BODY> 測試
獲取IP: ui
<script language="JavaScript"> function GetLocalIPAddr(){ var oSetting = null; var ip = null; try{ oSetting = new ActiveXObject("rcbdyctl.Setting"); ip = oSetting.GetIPAddress; if (ip.length == 0){ return "沒有鏈接到Internet"; } oSetting = null; }catch(e){ return ip; } return ip; } document.write(GetLocalIPAddr()+"<br/>") </script> spa
</BODY>
</HTML>
獲取客戶端處在網絡中的IP,前提是客戶得聯網。
[html] view plaincopy
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>JavaScript獲取客戶端IP[利用新浪接口]</title>
</head>
<body>
<script type="text/javascript" src="http://counter.sina.com.cn/ip/" charset="gb2312"></script> <!--獲取接口數據,注意charset -->
<script type="text/javascript">
document.writeln("IP地址:"+ILData[0]+"<br />"); //輸出接口數據中的IP地址
document.writeln("地址類型:"+ILData[1]+"<br />"); //輸出接口數據中的IP地址的類型
document.writeln("地址類型:"+ILData[2]+"<br />"); //輸出接口數據中的IP地址的省市
document.writeln("地址類型:"+ILData[3]+"<br />"); //輸出接口數據中的IP地址的
document.writeln("地址類型:"+ILData[4]+"<br />"); //輸出接口數據中的IP地址的運營商
</script>
</body>
</html>
調用VBS腳本,獲取計算機名(有些人不知道什麼是計算機名,簡單解釋就是它是這臺機器的物理名稱而不是你在用的那個用戶名)和登陸的用戶名。
[html] view plaincopy
<HTML>
<HEAD>
<TITLE>WMI Scripting HTML</TITLE>
</HEAD>
<BODY>
<script language=javascript>
var WshShell =new ActiveXObject("WScript.Shell");
document.write("計算機名 = "+ WshShell.ExpandEnvironmentStrings("%COMPUTERNAME%")+"<br/>");
document.write("登陸用戶名 = "+ WshShell.ExpandEnvironmentStrings("%USERNAME%")+"<br/>");
</script>
</BODY>
</HTML>
獲取計算機名、登陸的用戶名和域名(若是你加入了域,顯示你這臺機器處在哪一個域中)。
[html] view plaincopy
<HTML>
<HEAD>
<TITLE>WMI Scripting HTML</TITLE>
</HEAD>
<BODY>
<script language=javascript>
var wshNetwork = new ActiveXObject("WScript.Network");
document.write("域名 = "+ wshNetwork.UserDomain+"<br/>");
document.write("計算機名 = "+ wshNetwork.ComputerName+"<br/>");
document.write("登陸用戶名 = "+ wshNetwork.UserName+"<br/>");
</script>
</BODY>
</HTML>
可以獲取到局域網IP地址,本機的MAC,以及機器名(代碼來源網絡)。
[html] view plaincopy
<html>
<head>
<title></title>
</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.macAddress.value=sMacAddr;
myForm.ipAddress.value=sIPAddr;
myForm.hostName.value=sDNSName;
</script>