JS获取IP、MAC和主机名的几种方法

方法一(只针对IE且客户端的IE允许AcitiveX运行,通过平台:XP,SERVER03,2000):

获取客户端IP。

<HTML>  
<HEAD>  
<TITLE>GetLocalIP</TITLE>  
</HEAD>  
<BODY>  
获取IP:  
<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>   
</BODY>  
</HTML>  

方法二(所有的平台及浏览器):

获取客户端处在网络中的IP,前提是客户得联网。

<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>  

方法三(只针对IE且客户端的IE允许AcitiveX运行):

调用VBS脚本,获取计算机名(有些人不知道什么是计算机名,简单解释就是它是这台机器的物理名称而不是你在用的那个用户名)和登录的用户名。

<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>  

方法四(只针对IE且客户端的IE允许AcitiveX运行):

获取计算机名、登录的用户名和域名(如果你加入了域,显示你这台机器处在哪个域中)。

<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>  

方法五(只针对IE且客户端的IE允许AcitiveX运行):

能够获取到局域网IP地址,本机的MAC,以及机器名(代码来源网络)。

<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>  

 

posted @ 2014-03-28 10:30  Damon.Wu  阅读(781)  评论(0编辑  收藏  举报