Osmactool Java obtains the MAC address of the Local Machine (NIC address)

Source: Internet
Author: User
Package Cn.net. comsys. License. app;
/**
* @ Author Jif
* @ Version Creation Time: 09:14:52
* E-mail: liaojifeng@163.com
* Class description:
*
*
*
  */
Import Java. Io. bufferedreader;
Import Java. Io. ioexception;
Import Java. Io. inputstreamreader;
Import Java. util. properties;
Import Java. util. Logging. level;
Import Java. util. Logging. Logger;
Public Class Osmactool {

Public StaticString getmacaddress (){

String address = "";
String OS = system. getproperty ("OS. Name ");
// System. Out. println (OS );
If (OS! = Null ){
If (OS. startswith ("Windows ")){
Try {
Processbuilder Pb = New Processbuilder ("ipconfig", "/all ");
PROCESS p = Pb. Start ();
Bufferedreader BR = New Bufferedreader ( New Inputstreamreader (P. getinputstream ()));
String line;
While (Line = Br. Readline ())! = Null ){
If (Line. indexof ("physical address ")! =-1 ){
Int Index = line. indexof (":");
Address = line. substring (index + 1 );
Break ;
}
}
BR. Close ();
Return Address. Trim ();
} Catch (Ioexception e ){

}
} Else If (OS. startswith ("Linux ")){
Try {
Processbuilder Pb = New Processbuilder ("ifconfig ");
PROCESS p = Pb. Start ();
Bufferedreader BR = New Bufferedreader ( New Inputstreamreader (P. getinputstream ()));
String line;
While (Line = Br. Readline ())! =Null ){
Int Index = line. indexof ("hardware address ");
If (Index! =-1 ){
Address = line. substring (index + 4 );
Break ;
}
}
BR. Close ();
Return Address. Trim ();
} Catch (Ioexception ex ){
Logger. getlogger (osmactool. Class . Getname (). Log (level. Severe, Null , Ex );
}

}
}
Return Address;
}

Public Static VoidMain (string [] ARGs ){
System. Out. println ("" + osmactool. getmacaddress ());
}

}

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.