java获取服务器端网卡地址

本文介绍了一种使用Java程序来获取本地及远程计算机MAC地址的方法。提供了针对不同操作系统的解决方案,包括通过IP地址和主机名获取MAC地址的示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

 

package com.ctgusec.bean;    
   
import java.io.BufferedReader;    
import java.io.IOException;    
import java.io.InputStreamReader;    
import java.io.LineNumberReader;    
   
/**   
 *    
 * @author zhupan   
 * @version 1.0   
 */   
public class MACAddress {    
 public MACAddress() {    
 }    
   
 public static String getMACAddress() {    
  String address = "";    
  String os = System.getProperty("os.name");    
  if (os != null && os.startsWith("Windows")) {    
   try {    
    String command = "cmd.exe /c ipconfig /all";    
    Process p = Runtime.getRuntime().exec(command);    
    BufferedReader br = new BufferedReader(new InputStreamReader(p    
      .getInputStream()));    
    String line;    
    while ((line = br.readLine()) != null) {    
     if (line.indexOf("Physical Address") > 0) {    
      int index = line.indexOf(":");    
      index += 2;    
      address = line.substring(index);    
      System.out.println(address);    
      break;    
     }    
    }    
    br.close();    
    return address.trim();    
   } catch (IOException e) {    
   }    
  }    
  return address;    
 }    
   
 // 通过IP获取网卡地址    
 public static String getMacAddressIP(String remotePcIP) {    
  String str = "";    
  String macAddress = "";    
  try {    
   Process pp = Runtime.getRuntime().exec("nbtstat -A " + remotePcIP);    
   InputStreamReader ir = new InputStreamReader(pp.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 ex) {    
  }    
  return macAddress;    
 }    
   
 // 通过机器名获取网卡地址    
 public static String getMacAddressName(String remotePcIP) {    
  String str = "";    
  String macAddress = "";    
  try {    
   Process pp = Runtime.getRuntime().exec("nbtstat -a " + remotePcIP);    
   InputStreamReader ir = new InputStreamReader(pp.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 ex) {    
  }    
  return macAddress;    
 }    
   
 public static void main(String[] args) {    
  System.out.println(MACAddress.getMACAddress());             
        System.out.println(getMacAddressIP("192.168.175.200"));           
        System.out.println(getMacAddressName("527fefbedd5b43b."));    
   
 }    
}    
   
   
   
   
   
package com.ctgusec.bean;    
   
import java.io.IOException;    
import java.io.InputStream;    
import java.io.InputStreamReader;    
import java.io.LineNumberReader;    
   
/**   
 *    
 * @author zhupan   
 * @version 1.0   
 */   
   
public class GetMACAddress {    
 public String getMACAddress(String ipAddress) {    
  String str = "", strMAC = "", macAddress = "";    
  try {    
   Process pp = Runtime.getRuntime().exec("nbtstat -a " + ipAddress);    
   InputStreamReader ir = new InputStreamReader(pp.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) {    
      strMAC = str.substring(str.indexOf("MAC Address") + 14,    
        str.length());    
      break;    
     }    
    }    
   }    
  } catch (IOException ex) {    
   return "Can't Get MAC Address!";    
  }    
  //    
  if (strMAC.length() < 17) {    
   return "Error!";    
  }    
  macAddress = strMAC.substring(0, 2) + ":" + strMAC.substring(3, 5)    
    + ":" + strMAC.substring(6, 8) + ":" + strMAC.substring(9, 11)    
    + ":" + strMAC.substring(12, 14) + ":"   
    + strMAC.substring(15, 17);    
  //    
  return macAddress;    
 }    
   
 public static void main(String[] args) {    
  GetMACAddress getMACAddress = new GetMACAddress();    
  System.out.println(getMACAddress.getMACAddress("192.168.175.66"));    
   
  try {    
   java.lang.Process proc = Runtime.getRuntime().exec("ipconfig /all");    
   InputStream istr = proc.getInputStream();    
   byte[] data = new byte[1024];    
   istr.read(data);    
   String netdata = new String(data);    
   System.out.println("Your Mac Address=" + procAll(netdata));    
  } catch (IOException e) {    
   System.out.println("error=" + e);    
  }    
 }    
   
 public static String procAll(String str) {    
  return procStringEnd(procFirstMac(procAddress(str)));    
 }    
   
 public static String procAddress(String str) {    
  int indexof = str.indexOf("Physical Address");    
  if (indexof > 0) {    
   return str.substring(indexof, str.length());    
  }    
  return str;    
 }    
   
 public static String procFirstMac(String str) {    
  int indexof = str.indexOf(":");    
  if (indexof > 0) {    
   return str.substring(indexof + 1, str.length()).trim();    
  }    
  return str;    
 }    
   
 public static String procStringEnd(String str) {    
  int indexof = str.indexOf("/r");    
  if (indexof > 0) {    
   return str.substring(0, indexof).trim();    
  }    
  return str;    
 }    
}  

 

 

 

 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

折腾数据折腾代码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值