Obtain the MAC address and IP address of the client Nic. JS Code

Source: Internet
Author: User

In a B/S system, we often need to obtain some information about the client, such as IP and MAC, for identity authentication. It is easy to obtain the MAC address of the server, but it takes too much effort to obtain the MAC address of the client. The common practice is to call Win32API or directly call the nbtstat command, there are a lot of problems with this, and the other method is to directly use the client script. Here we use Javascript. The advantage of this is that it does not need to be processed by the server, but can be obtained by the client itself, it is passed to the server, and the speed and reliability are better than those obtained on the server.
The specific html and javascript implementations are as follows::
Copy codeThe Code is as follows:
<HTML>
<HEAD>
<TITLE> WMI Scripting HTML </TITLE>
<META http-equiv = Content-Type content = "text/html; charset = gb2312">
<SCRIPT language = JScript event = "OnCompleted (hResult, pErrorObject, pAsyncContext)" for = foo>
Document.forms000002.16.txt MACAddr. value = unescape (MACAddr );
Document.forms000002.16.txt IPAddr. value = unescape (IPAddr );
Document.forms000002.16.txt DNSName. value = unescape (sDNSName );
// Document. formbar. submit ();
</SCRIPT>
<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>
<META content = "MSHTML 6.00.2800.1106" name = GENERATOR> </HEAD>
<BODY>
<OBJECT id = locator classid = CLSID: 76A64158-CB41-11D1-8B02-00600806D9B6 VIEWASTEXT> </OBJECT>
<OBJECT id = foo classid = CLSID: 75718C9A-F029-11d1-A1AC-00C04FB6C223> </OBJECT>
<SCRIPT language = JScript>
Var service = locator. ConnectServer ();
Var MACAddr;
Var IPAddr;
Var DomainAddr;
Var sDNSName;
Service. Security _. ImpersonationLevel = 3;
Service. InstancesOfAsync (foo, 'win32 _ networkadapterconfiguration ');
</SCRIPT>
<FORM id = formfoo name = formbar method = post>
<INPUT type = "text" name = txtMACAddr>
<INPUT type = "text" name = txtIPAddr>
<INPUT type = "text" name = txtDNSName>
</FORM>
</BODY>
</HTML>

In fact, the most important thing is to use two ActiveX:
Copy codeThe Code is as follows:
<OBJECT id = locator classid = CLSID: 76A64158-CB41-11D1-8B02-00600806D9B6 VIEWASTEXT> </OBJECT>
<OBJECT id = foo classid = CLSID: 75718C9A-F029-11d1-A1AC-00C04FB6C223> </OBJECT>

However, these two ActiveX methods are provided by the system and do not need to be downloaded or registered.
The next step is to use scripts to interact with ActiveX. scripts can be JavaScript or Vbs.

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.