JS to get a summary of the seven methods of client IP address MAC and host name

  • 2020-03-30 03:32:49
  • 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:

One, using JS to obtain a few methods of client IP

Method 1 (IE only for IE and client side IE allows AcitiveX to run through platform: XP, SERVER03, 2000).
Get the client IP code:


<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. What use is sina interface.


<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 />");             //The IP address in the output interface data is
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 />");         //Output
of the IP address in the interface data document.writeln(" Address type: "+ILData[4]+"<br />");         //The operator
of the IP address in the output interface data </script>
</body>
</html>

Method 3 (all platforms and browsers) :
Used search fox interface


<script src="http://pv.sohu.com/cityjson?ie=utf-8"></script
<script type="text/javascript"> 
document.write(returnCitySN["cip"]+','+returnCitySN["cname"]) 
</script>

Method 4: IP query interface of Pacific computer network:

(link: http://whois.pconline.com.cn/? IP = 0.0.0.0)

Replace 0.0.0.0 with an IP address, and there is other irrelevant content on the page that tells us which interfaces can be invoked, interface call parameters, and usage methods

Through the above js interface call can be used to determine which city belongs to, directly display the relevant information of the city, for the site that needs to switch cities, the first time to determine the user source is very helpful.

Second, using JS to get the computer name, MAC address, LAN IP

Method 1 (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 2 (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 3 (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: