Three simple methods: how to obtain the client MAC address

Source: Internet
Author: User

Method 1:

Call the Windows doscommand to read the MAC address from the output:

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(cmd.exe c (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" and "arp-a"... We can get the mac list... haha !!


Disadvantage: only the MAC address of the server can be obtained. If you want to obtain the MAC address of the client, you need to use the Applet. Only for the MS-WIN system.

 

Method 2:

You can use JS or vbscript to call the WMI interface to obtain the MAC address of the Client.

<Html>
<Head>
<Title> WMI Scripting HTML </title>
<Script FOR = "foo" EVENT = "OnCompleted (hResult, pErrorObject, pAsyncContext)" LANGUAGE = "JScript">

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

For more information about WMI, see 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. What we had to do with heavyweight programming tools was now done with js/vbscript.


Obtain the MAC address of multiple NICs:

If (objObject. MACAddress! = Null & objObject. MACAddress! = "Undefined "){
MACAddr = objObject. MACAddress;
Alert (MACAddr );
}


Disadvantages: ActiveX support is required, which is effective for MS-WIN system.

Method 3:

To send a UDP query with 137 ports:

For a WINDOWS client (which is converted to a server role upon retrieval), the NETBIOS protocol is on port 137, so we only need to send a UDP query to port 137, you can obtain all the NIC addresses of the ENI after obtaining the return value.

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.