Five ways JS gets IP MAC and host names

  • 2020-03-29 23:46:57
  • OfStack

Today in the JS (javascript) to get the client IP small program, the Internet search, many in the current system and browser are invalid, very helpless, in Chrome, FireFox rarely get the direct use of ActiveX to get IP JS script. The following code, which I have tested on all windowsNT5.0 and above, is given:

Method 1 (IE only for IE and client side IE allows AcitiveX to run through platform: XP, SERVER03, 2000) :
Get the client IP.
 
<HTML> 
<HEAD> 
<TITLE>GetLocalIP</TITLE> 
</HEAD> 
<BODY> 
 To obtain 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 " Not connected to Internet"; } oSetting = null; }catch(e){ return ip; } return ip; } document.write(GetLocalIPAddr()+"<br/>") </script> 
</BODY> 
</HTML> 

Method 2 (all platforms and browsers) :
Get the client in the network IP, the premise is that the client network.
 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"> 
<title>JavaScript Acquire client IP[ Use sina interface ]</title> 
</head> 
<body> 
<script type="text/javascript" src="http://counter.sina.com.cn/ip/" charset="gb2312"></script> <!-- Get the interface data, notice charset --> 
<script type="text/javascript"> 
document.writeln("IP Address: "+ILData[0]+"<br />"); //Output the IP address in the interface data
document.writeln(" Address type: "+ILData[1]+"<br />"); //Type of IP address in the output interface data
document.writeln(" Address type: "+ILData[2]+"<br />"); //Output interface data in the IP address of the provinces and cities
document.writeln(" Address type: "+ILData[3]+"<br />"); //Of the IP address in the output interface data
document.writeln(" Address type: "+ILData[4]+"<br />"); //The operator that outputs the IP address in the interface data
</script> 
</body> 
</html> 

Method 3 (IE only for IE and client side IE allows AcitiveX to run) :
Call the VBS script, get the computer name (some people don't know what a computer name is, simply explain that it is the physical name of the machine and not the user name you are using) and the login user name.
 
<HTML> 
<HEAD> 
<TITLE>WMI Scripting HTML</TITLE> 
</HEAD> 
<BODY> 
<script language=javascript> 
var WshShell =new ActiveXObject("WScript.Shell"); 
document.write(" Computer name  = "+ WshShell.ExpandEnvironmentStrings("%COMPUTERNAME%")+"<br/>"); 
document.write(" Login username  = "+ WshShell.ExpandEnvironmentStrings("%USERNAME%")+"<br/>"); 
</script> 
</BODY> 
</HTML> 

Method 4 (IE only for IE and client side IE allows AcitiveX to run) :
Gets the name of the computer, the login username, and the domain name (if you join the domain, show which domain your machine is in).
 
<HTML> 
<HEAD> 
<TITLE>WMI Scripting HTML</TITLE> 
</HEAD> 
<BODY> 
<script language=javascript> 
var wshNetwork = new ActiveXObject("WScript.Network"); 
document.write(" The domain name  = "+ wshNetwork.UserDomain+"<br/>"); 
document.write(" Computer name  = "+ wshNetwork.ComputerName+"<br/>"); 
document.write(" Login username  = "+ wshNetwork.UserName+"<br/>"); 
</script> 
</BODY> 
</HTML> 

Method 5 (IE only for IE and client side IE allows AcitiveX to run) :
Access to LAN IP address, local MAC, and machine name (code source network).
 
<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 Address: <input type="text" name="macAddress"> 
<br/>IP Address: <input type="text" name="ipAddress"> 
<br/> Host name: <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> 

Related articles: