62,614
社区成员
发帖
与我相关
我的任务
分享
package csdn.impulsehu.may;
import java.net.InetAddress;
import java.net.NetworkInterface;
public class MacAddressTest {
public static void main(String[] args) {
System.out.println(getMacAddress());
}
private static String getMacAddress() {
InetAddress ia = null;
NetworkInterface ni = null;
byte[] bytes = null;
String macAdd = "";
try {
ia = InetAddress.getLocalHost();
ni = NetworkInterface.getByInetAddress(ia);
bytes = ni.getHardwareAddress();
for(int i=0; i<bytes.length; i++) {
if(bytes[i] != 0) {
if(bytes[i] >= 0) {
macAdd = macAdd + Integer.toHexString(bytes[i]) + "-";
} else {
macAdd = macAdd + Integer.toHexString(bytes[i]).substring(6) + "-";
}
} else {
macAdd = macAdd + "00" + "-";
}
}
} catch (Exception e) {
e.printStackTrace();
}
return macAdd.substring(0, macAdd.length()-1).toUpperCase();
}
}
Process p = Runtime.getRuntime().exec("ipconfig /all");
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
for (String s=br.readLine(); s!=null; s=br.readLine()) {
if (s.indexOf("Physical") >= 0) {
System.out.println(s);
}
}
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
/**
* @ClassName: MacAddress
* @Description:
* @author LiangO
* @date 2011-05-25 16:41:51
*
*/
public class MacAddress {
/**
* @Title: getMacAddress
* @Description: TODO(这里用一句话描述这个方法的作用)
* @param @param string
* @param @return 设定文件
* @return char[] 返回类型
* @throws
*/
private static String getMacAddress(String ip) {
String str = "";
String macAddress = "";
try {
Process p = Runtime.getRuntime().exec("nbtstat -A " + ip);
InputStreamReader ir = new InputStreamReader(p.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
for (int i = 1; i < 100; i++) {
str = input.readLine();
if (str != null) {
if (str.indexOf("MAC Address") > 1) {
macAddress = str.substring(
str.indexOf("MAC Address") + 14, str.length());
break;
}
}
}
} catch (IOException e) {
e.printStackTrace(System.out);
}
return macAddress;
}
public static void main(String[] args) {
System.out.println(getMacAddress("192.168.1.2"));
}
}