package getmac;import java.net.InetAddress;import java.net.NetworkInterface;import java.net.SocketException;import java.net.UnknownHostException;import java.util.logging.Level;import java.util.logging.Logger;/**** @author flowercatswets*/public class GetMac {public static void main(String[] args) {byte[] mac;try {//取得本機的IPInetAddress address = InetAddress.getLocalHost();//依據取得的IP取得網卡NetworkInterface ni = NetworkInterface.getByInetAddress(address);//取出網卡macmac = ni.getHardwareAddress();//把網卡mac格式化輸出 xx-xx-xx-xx-xx-xxfor (int i = 0; i < mac.length; i++) {System.out.format("%02X%s", mac[i], (i < mac.length - 1) ? "-": "");}System.out.print("\n");} catch (UnknownHostException | SocketException ex) {Logger.getLogger(GetMac.class.getName()).log(Level.SEVERE, null, ex);}}}
參考來源:
https://code.google.com/p/rogued/source/browse/trunk/src/DHCPUtility.java?r=6
沒有留言:
張貼留言