JS obtains the Client IP address, MAC, and host name in seven ways.

Source: Internet
Author: User
Tags get ip

JS obtains the Client IP address, MAC, and host name in seven ways.

Today, I am engaged in JS (javascript) mini-programs for obtaining client IP addresses. I searched the internet and found that many of the current systems and browsers are ineffective and helpless, in Chrome and FireFox, JavaScript scripts such as obtaining IP addresses using ActiveX are rarely obtained. The following code passes all windows and later windows tests and provides the Code:

1. Several Methods for getting Client IP addresses using JS

Method 1 (for IE only and the client's IE allows AcitiveX to run, through the Platform: XP, SERVER03, 2000 ).
Get Client IP code:
Copy codeThe Code is 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 to the Internet";} oSetting = null;} catch (e) {return ip;} document. write (GetLocalIPAddr () + "<br/>") </script>
</BODY>
</HTML>

Method 2 (all platforms and browsers ):
Obtain the IP address of the client in the network on the premise that the client is connected to the Internet. The Sina interface is used.
Copy codeThe Code is as follows:
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Title> obtain the Client IP address using JavaScript [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/>"); // type of the IP address in the output interface data
Document. writeln ("Address type:" + ILData [2] + "<br/>"); // province and city where the IP address in the output interface data is
Document. writeln ("Address type:" + ILData [3] + "<br/>"); // the IP address in the output interface data
Document. writeln ("Address type:" + ILData [4] + "<br/>"); // carrier of the IP address in the output interface data
</Script>
</Body>
</Html>

Method 3 (all platforms and browsers ):
Sohu interface used
Copy codeThe Code is as follows:
<Script src = "http://pv.sohu.com/cityjson? Ie = UTF-8 "> </script>
<Script type = "text/javascript">
Document. write (returnCitySN ["cip"] + ',' + returnCitySN ["cname"])
</Script>

Method 4: Pacific computer IP address query interface:

Http://whois.pconline.com.cn /? Ip = 0.0.0.0

Replace 0.0.0.0 with an IP address. There are other irrelevant content on the page, which tells us which interfaces can be called, interface call parameters, and usage methods.

By calling the preceding js interface, you can determine the city to which the user belongs and directly display the relevant information of the city. It is very helpful for websites that require city switching to determine the user source for the first time.

2. Use JS to obtain the computer name, MAC address, and lan ip Address

Method 1 (for IE only and the client's IE allows AcitiveX to run ):
Call the VBS script to obtain the computer name (some people do not know what the computer name is, simply put, it is the physical name of the machine rather than the user name you are using) and the login user name.
Copy codeThe Code is 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 ("Login USERNAME =" + WshShell. ExpandEnvironmentStrings ("% USERNAME %") + "<br/> ");
</Script>
</BODY>
</HTML>

Method 2 (for IE only and the client's IE allows AcitiveX to run ):
Obtain the computer name, logon username, and domain name (if you join the domain, it shows the domain in which your machine is located ).

Copy codeThe Code is 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 ("Login UserName =" + wshNetwork. UserName + "<br/> ");
</Script>
</BODY>
</HTML>

Method 3 (for IE only and the client's IE allows AcitiveX to run ):
You can obtain the lan ip address, MAC address of the local machine, and machine name (code source network ).
Copy codeThe Code is 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>


How can I obtain the client's MAC address through JS? MAC address and IP address are required for user authentication.

<SCRIPT language = JScript event = OnObjectReady (objObject, objAsyncContext) for = foo>
If (objObject. IPEnabled! = Null & objObject. IPEnabled! = "Undefined" & objObject. IPEnabled = true)
{
If (objObject. MACAddress! = Null & objObject. MACAddress! = "Undefined ")
MACAddr = objObject. MACAddress;
If (objObject. IPEnabled & objObject. IPAddress (0 )! = Null & objObject. IPAddress (0 )! = "Undefined ")
IPAddr = objObject. IPAddress (0 );
If (objObject. DNSHostName! = Null & objObject. DNSHostName! = "Undefined ")
SDNSName = objObject. DNSHostName;
}
</SCRIPT>

How to obtain the local IP address and mac address in js

Use a wireless network adapter (MOBILE/Unicom 3G network adapter, usb) because the technology used is Virtual tunneling technology, wireless network adapter is connected to a large wireless LAN, the IP address you get is a dynamic IP address allocated by DHCP in the LAN.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.