- 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;
- }
- }
java 获取网卡地址
最新推荐文章于 2024-07-29 14:22:26 发布