PDA

View Full Version : گرفتن مک آدرس سیستم



parnian~parnian
جمعه 05 مهر 1392, 21:37 عصر
سلام دوستان
من برای گرفتن مک ادرس سیستم از تکه کد زیر استفاده می کنم :
private static String getMacAddress() throws IOException {
String command = "ipconfig /all";
Process pid = Runtime.getRuntime().exec(command);
BufferedReader in = new BufferedReader(new InputStreamReader(pid.getInputStream()));
Pattern p = Pattern.compile(".*Physical Address.*: (.*)");
while (true) {
String line = in.readLine();
if (line == null)
break;
Matcher m = p.matcher(line);
if (m.matches()) {
return m.group(1);
}
}
return "";
}

ولی مشکل اینه وقتی به اینترنت وصل باشه مک ادرس رو نمیده .......... کسی از دوستان می دونه چطوری میشه وقتی سیستم به اینترنت وصله مک ادرس سیستم رو گرفت ؟

cups_of_java
شنبه 06 مهر 1392, 18:10 عصر
این کد درستی برای اینکار نیست
از این استفاده کن:
public class App{

public static void main(String[] args){

InetAddress ip;
try {

ip = InetAddress.getLocalHost();
System.out.println("Current IP address : " + ip.getHostAddress());

NetworkInterface network = NetworkInterface.getByInetAddress(ip);

byte[] mac = network.getHardwareAddress();

System.out.print("Current MAC address : ");

StringBuilder sb = new StringBuilder();
for (int i = 0; i < mac.length; i++) {
sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
}
System.out.println(sb.toString());

} catch (UnknownHostException e) {

e.printStackTrace();

} catch (SocketException e){

e.printStackTrace();

}

}

}

parnian~parnian
شنبه 06 مهر 1392, 21:13 عصر
ممنون از پاسختون ولی هیچ فرقی نداره این کد هم قبلا تست کردم وقتی به اینترنت وصل باشی این هم مک ادرس رو نمیده

parnian~parnian
دوشنبه 15 مهر 1392, 22:27 عصر
بچه ها کسی واسه این راه حلی ندارههههههههههههههه ؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟ ؟؟؟؟؟؟؟؟؟؟؟؟؟؟

spiderman200700
دوشنبه 15 مهر 1392, 23:07 عصر
من قبلا چندین بار از کدی که جناب cups_of_java قرار دادن استفاده کردم. اصلا به اینترنت ربطی نداره. برای من خوب کار میکنه

parnian~parnian
شنبه 20 مهر 1392, 22:17 عصر
اره والا چی بگم واسه من که اینطوریه وقتی به نت وصل میشم دیگه برنمیگردونه مک ادرسو :افسرده: