How to obtain a Client MAC address (three methods)

Source: Internet
Author: User

Method One:

Call the Windows DOS command to read the MAC address from the output result:

public static String getmacaddress () {

String address = "";
String OS = System.getproperty ("Os.name");
if (OS! = null && os.startswith ("Windows")) {
try {
String command = "cmd.exe/c Ipconfig/all";
Process p = runtime.getruntime (). exec (command);
BufferedReader br =
New BufferedReader (
New InputStreamReader (P.getinputstream ()));
String Line;
while (line = Br.readline ()) = null) {
if (Line.indexof ("Physical Address") > 0) {
int index = Line.indexof (":");
Index + = 2;
Address = line.substring (index);
Break
}
}
Br.close ();
return Address.trim ();
}
catch (IOException e) {}
}
return address;
}

We can replace the "ipconfig" to "ping x.x.x.x" and "arp-a" ... We can get the Mac list...haha!!


Disadvantage: Only server-side MAC addresses can be obtained. If you want to get the MAC address of the client, you need an applet. Only for Ms-win systems.

Method Two:

You can use JS or VBScript to invoke the WMI interface to get the MAC address of the client side.

<TITLE>WMI Scripting html</title>
<script for= "foo" event= "oncompleted (Hresult,perrorobject, Pasynccontext)" language= "JScript" >

             Document.forms[0].txtmacaddr.value=unescape (MACADDR);
             document.forms[0].txtipaddr.value= Unescape (IPADDR);
             Document.forms[0].txtdnsname.value =unescape (Sdnsname);
            //document.formbar.submit ();
        </script>
 
 <script for= "foo" event= " OnObjectReady (objobject,objasynccontext) "language=" JScript;

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>
 </ Head>
 <body>
  <object classid= "Clsid:76a64158-cb41-11d1-8b02-00600806d9b6" id= " Locator "VIEWASTEXT>
  </object>
  <object classid=" CLSID : 75718c9a-f029-11d1-a1ac-00c04fb6c223 "id=" foo ",
  </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 method= "POST" action= "nicpost.asp" id= "Formfoo" name= "Formbar" >

<input type=text name= "Txtmacaddr" >
<input type=text name= "Txtipaddr" >
<input type=text name= "Txtdnsname" >

</form>
</body>

For more information about WMI, refer to MSDN:
Http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/wmi_tasks_for_scripts_and_applications.asp

In all fairness, WMI is very powerful. Things that need to be done using a heavyweight programming tool can now be done with Js/vbscript.


To obtain the MAC address of multiple network cards:

if (objobject.macaddress! = NULL && objobject.macaddress! = "undefined") {
MACADDR = objobject.macaddress;
alert (MACADDR);
}


Disadvantage: ActiveX support is required. Valid for Ms-win system.

Method Three:

To send a UDP query with 137 ports:

The client of the Windows platform (when it gets converted to the server-side role), the NetBIOS protocol is on 137 ports, we simply send a UDP query to its 137 port, get its return value and get all of its network card address

. Net Technology Discussion Group 4151320

Welcome everyone to join!!!

How to obtain a Client MAC address (three methods)

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.