Read network MAC address

Source: Internet
Author: User

// Read the Ethernet MAC in the directory CAT/sys/class/NET/eht0/address

// Read the MAC address of wifi or current network connection: CAT/sys/class/NET/wlan0/address

Public static string getmac (){

String macserial = NULL;
Try {
Process pp = runtime.getruntime(cmd.exe C (
"Cat/sys/class/NET/eht0/address ");
Inputstreamreader IR = new inputstreamreader (pp. getinputstream ());
Linenumberreader input = new linenumberreader (IR );
String STR = input. Readline ();
If (STR! = NULL ){
Macserial = Str. Trim (); // remove space
}

} Catch (ioexception ex ){
// Assign the default value
Macserial = "00: 00: 00: 00 ";
Ex. printstacktrace ();
}
Return macserial. Replace (":","");

}

// You can also use Linux commands to obtain

/*
* Public static string getmacaddress () {string result = ""; string MAC =
* ""; Result = callcmd ("busybox ifconfig", "hwaddr ");
*
* // If the returned result = NULL, the network cannot be obtained if (result = NULL) {return
* "Network error. Check the network ";}
*
* // Parse the row of Data // For example: eth0 link encap: Ethernet hwaddr 00: 16: E8: 3E: DF: 67 If
* (Result. Length ()> 0 & result. Contains ("hwaddr") = true) {MAC =
* Result. substring (result. indexof ("hwaddr") + 6, result. Length ()-1 );
* Log. I ("test", "Mac:" + Mac + "Mac. Length:" + Mac. Length ());
*
* If (Mac. Length ()> 1) {MAC = Mac. replaceall ("", ""); Result = "";
* String [] TMP = Mac. Split (":"); For (INT I = 0; I <TMP. length; ++ I ){
* Result + = TMP [I];} log. I ("test", result + "result. Length:" +
* Result. Length ();} return result ;}
*
* Public static string callcmd (string cmd, string filter) {string result =
* ""; String line = ""; try {process proc =
* Runtime.getruntime(cmd.exe C (CMD); inputstreamreader is = new
* Inputstreamreader (Proc. getinputstream (); bufferedreader BR = new
* Bufferedreader (is );
*
* // Execute the command cmd. Only the while (line = Br. Readline () containing the filter in the result is obtained ())! = NULL &&
* Line. Contains (filter) = false) {// result + = line; log. I ("test ",
* "Line:" + line );}
*
* Result = line; log. I ("test", "Result:" + result);} catch (exception E)
* {E. printstacktrace ();} return result ;}
*/

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.