Three methods for obtaining the MAC address of the client Nic using Java/jsp

Source: Internet
Author: User

There are about three methods to obtain the MAC address of the client (IE) Nic from Java/JSP.

1. Execute ipconfig on the client using commands.

2. ActiveX Method

3. How to send query commands to port 137

 

Introduction:

The first method is to block the command that does not know why the MAC address is obtained when it is actually used. And the speed is the slowest of the three methods.

The Code is as follows:

String sip = "";
String SMAC = "";
SIP = request. getheader ("X-forwarded-");
If (SIP = NULL | sip. Length () = 0 | "unknown". equalsignorecase (SIP ))
{
SIP = request. getheader ("proxy-client-IP ");
}
If (SIP = NULL | sip. Length () = 0 | "unknown". equalsignorecase (SIP ))
{
SIP = request. getheader ("wl-proxy-client-IP ");
}
If (SIP = NULL | sip. Length () = 0 | "unknown". equalsignorecase (SIP ))
{
SIP = request. getremoteaddr ();
}

If (! "127.0.0.1". Equals (SIP) // filter out the Local Machine
{
Process = runtime.getruntime(cmd.exe C ("nbtstat-a" + SIP );
Inputstreamreader IR = new inputstreamreader (process. getinputstream ());
Linenumberreader input = new linenumberreader (IR );
String line;
While (line = input. Readline ())! = NULL)
If (line. indexof ("MAC address")> 0)
{
SMAC = line. substring (line. indexof ("-")-2 );
}

 

The second method is obtained using ActiveX, which is faster than the first method, but must reduce the security settings. Otherwise, the script will not be executed.

The iesettings are as follows:

1. Open IE browser
2. Tools> Internet Options> Security
3. Custom Level (c )...
4. initialize and execute the script for the ActiveX control that is not marked as a secure script.
Select "Start (Insecure)"-> click "yes" each time. We recommend that you do not access the Internet or you have security issues.
Select the prompt> click "yes" each time.
5. Click "OK"-> "OK ".
6. Restart IE to apply the settings.

 

Code

<Script language = JScript event = "oncompleted (hresult, perrorobject, pasynccontext)" for = Foo>
Document. Forms [0]. usermacaddr. value = Unescape (macaddr );
</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;
}
</SCRIPT>
<Meta content = "mshtml 6.00.2800.1106" name = generator>
...

<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;
Service. Security _. impersonationlevel = 3;
Service. instancesofasync (Foo, 'win32 _ networkadapterconfiguration ');
</SCRIPT>
... <TD Height = "20" align = "center"> MARK: </TD>
<TD> <input value = "no" name = "usermacaddr" size = "20" style = "width: 150px" readonly = "yes"> </TD>

 

The third method is to use the UDP protocol to send the query Mac command and parse the Mac in the return value. I personally think this is the best way (temporarily) the fastest speed

<%
String SMAC = "";
String sip = request. getheader ("X-forwarded-");
If (SIP = NULL | sip. Length () = 0 | "unknown". equalsignorecase (SIP )){
SIP = request. getheader ("proxy-client-IP ");
}
If (SIP = NULL | sip. Length () = 0 | "unknown". equalsignorecase (SIP )){
SIP = request. getheader ("wl-proxy-client-IP ");
}
If (SIP = NULL | sip. Length () = 0 | "unknown". equalsignorecase (SIP )){
SIP = request. getremoteaddr ();
}
Udpgetclientmacaddr UMAC = new udpgetclientmacaddr (SIP );
SMAC = UMAC. getremotemacaddr ();
%>

...

<Input value = <% = SMAC %> name = "usermacaddr" size = "20" style = "width: 150px" readonly = "yes">

 

Udpgetclientmacaddr. Java

Package CCH;
Import java. Io .*;
Import java.net .*;

Public class udpgetclientmacaddr {
Private string sremoteaddr;
Private int iremoteport = 137;
Private byte [] buffer = new byte [1, 1024];
Private datagramsocket DS = NULL;

Public udpgetclientmacaddr (string straddr) throws exception {
Sremoteaddr = straddr;
DS = new datagramsocket ();
}

Protected final encrypt rampacket send (final byte [] bytes) throws ioexception {
Required rampacket dp = new datagrampacket (bytes, bytes. length, inetaddress. getbyname (sremoteaddr), iremoteport );
DS. Send (DP );
Return DP;
}

Protected final datagrampacket receive () throws exception {
Datagrampacket dp = new datagrampacket (buffer, buffer. Length );
DS. Receive (DP );
Return DP;
}

Protected byte [] getquerycmd () throws exception {
Byte [] t_ns = new byte [50];
T_ns [0] = 0x00;
T_ns [1] = 0x00;
T_ns [2] = 0x00;
T_ns [3] = 0x10;
T_ns [4] = 0x00;
T_ns [5] = 0x01;
T_ns [6] = 0x00;
T_ns [7] = 0x00;
T_ns [8] = 0x00;
T_ns [9] = 0x00;
T_ns [10] = 0x00;
T_ns [11] = 0x00;
T_ns [12] = 0x20;
T_ns [13] = 0x43;
T_ns [14] = 0x4b;

For (INT I = 15; I <45; I ++)
{
T_ns [I] = 0x41;
}

T_ns [45] = 0x00;
T_ns [46] = 0x00;
T_ns [47] = 0x21;
T_ns [48] = 0x00;
T_ns [49] = 0x01;
Return t_ns;
}

Protected final string getmacaddr (byte [] brevdata) throws exception {

Int I = brevdata [56] * 18 + 56;
String saddr = "";
Stringbuffer sb = new stringbuffer (17 );

For (Int J = 1; j <7; j ++)
{
Saddr = integer. tohexstring (0xff & brevdata [I + J]);
If (saddr. Length () <2)
{
SB. append (0 );
}
SB. append (saddr. touppercase ());
If (j <6) sb. append (':');
}
Return sb. tostring ();
}

Public final void close (){
Try
{
DS. Close ();
}
Catch (exception ex ){
Ex. printstacktrace ();
}
}

Public final string getremotemacaddr () throws exception {
Byte [] bqcmd = getquerycmd ();
Send (bqcmd );
Datagrampacket dp = receive ();
String SMAC = getmacaddr (DP. getdata ());
Close ();

Return SMAC;
}
}

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.