Three Methods for JS to Obtain Client IP Address, MAC and Host Name

This article mainly introduces js JS itself does not support acquiring information such as IP address. This paper implements it by other methods. Friends who need it can refer to the following.

Today we are working on JS( JavaScript ) After searching the internet, many of the small programs to get client IP are ineffective in the current system and browser, but they are very helpless. In Chrome and FireFox, there are few JS scripts that directly use ActiveX to get IP and so on. The following code is for all Windows NT 5.0 and above systems test Pass, give the code:

I. Several Methods of Obtaining Client IP Using JS

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

Copy the code as follows:

 <HTML>
 <HEAD>
 <TITLE>GetLocalIP</TITLE>
 </HEAD>
 <BODY>
 //Get 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 Internet"; } oSetting = null; }catch(e){ return ip; } return ip; } document.write(GetLocalIPAddr()+"<br/>") </script> 
</BODY>
 </HTML>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

Method 2 (all platforms and browsers):
To get the IP of the client in the network, the premise is that the client has to be connected to the network. Sina interface is used.

Copy the code as follows:

<html>
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
 <title>JavaScript Get Client IP[Using Sina Interface]</title>
 </head>
 <body>
 <script type="text/javascript" src="http://counter.sina.com.cn/ip/" charset="gb2312"></script>       <!--Get interface data, note charset -->
 <script type="text/javascript"> 
document.writeln("IP Address:"+ILData[0]+"<br />");             //IP Address in Output Interface Data 
document.writeln("Address type:"+ILData[1]+"<br />");         //Types of IP Addresses in Output Interface Data 
document.writeln("Address type:"+ILData[2]+"<br />");         //Provinces and Cities with IP Address in Output Interface Data
 document.writeln("Address type:"+ILData[3]+"<br />");         //The IP address in the output interface data
 document.writeln("Address type:"+ILData[4]+"<br />");         //Operator of IP Address in Output Interface Data
 </script>
 </body>
 </html>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

Method 3 (all platforms and browsers):
Sohu Interface Used

Copy the code as follows:

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

Method 4: Pacific Computer Network IP Query Interface:

http://whois.pconline.com.cn/?ip=0.0.0.0

Change 0.0.0 to IP address. There are other irrelevant contents on the page. These contents tell us which interfaces can be invoked, the parameters of interface invocation and the way to use them.

Through the js interface call above, we can determine which city to belong to, and directly display the relevant information of the city. It is very helpful to determine the source of users for the first time for websites requiring city switching.

2. Using JS to Get Computer Name, MAC Address and LAN IP

Method 1 (IE only for IE and client IE allows AcitiveX to run):
Call the VBS script to 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 rather than the user name you are using) and the login user name.

Copy the code as follows:

 <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("Username = "+ WshShell.ExpandEnvironmentStrings("%USERNAME%")+"<br/>");
 </script>
 </BODY>
 </HTML>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

Method 2 (IE only for IE and client IE allows AcitiveX to run):
Get the computer name, login username and domain name (if you join the domain, show which domain your machine is in).

Copy the code as follows:

<HTML>
 <HEAD>
 <TITLE>WMI Scripting HTML</TITLE>
 </HEAD>
 <BODY>
 <script language=javascript>
 var wshNetwork = new ActiveXObject("WScript.Network");
 document.write("domain name       = "+ wshNetwork.UserDomain+"<br/>");
 document.write("Computer name   = "+ wshNetwork.ComputerName+"<br/>");
 document.write("Username = "+ wshNetwork.UserName+"<br/>");
 </script>
 </BODY>
 </HTML>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

Method 3 (IE only for IE and client IE allows AcitiveX to run):
It can get the IP address of the LAN, the MAC of the local machine, and the machine name (code source network).

Copy the code as follows:

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

Keywords: IE Javascript network Mac

Added by dallasx on Sat, 22 Jun 2019 02:20:59 +0300