`
温柔一刀
  • 浏览: 856879 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

java获取服务器端网卡地址

    博客分类:
  • java
阅读更多
java 代码
  1. package com.ctgusec.bean;   
  2.   
  3. import java.io.BufferedReader;   
  4. import java.io.IOException;   
  5. import java.io.InputStreamReader;   
  6. import java.io.LineNumberReader;   
  7.   
  8. /**  
  9.  *   
  10.  * @author zhupan  
  11.  * @version 1.0  
  12.  */  
  13. public class MACAddress {   
  14.  public MACAddress() {   
  15.  }   
  16.   
  17.  public static String getMACAddress() {   
  18.   String address = "";   
  19.   String os = System.getProperty("os.name");   
  20.   if (os != null && os.startsWith("Windows")) {   
  21.    try {   
  22.     String command = "cmd.exe /c ipconfig /all";   
  23.     Process p = Runtime.getRuntime().exec(command);   
  24.     BufferedReader br = new BufferedReader(new InputStreamReader(p   
  25.       .getInputStream()));   
  26.     String line;   
  27.     while ((line = br.readLine()) != null) {   
  28.      if (line.indexOf("Physical Address") > 0) {   
  29.       int index = line.indexOf(":");   
  30.       index += 2;   
  31.       address = line.substring(index);   
  32.       System.out.println(address);   
  33.       break;   
  34.      }   
  35.     }   
  36.     br.close();   
  37.     return address.trim();   
  38.    } catch (IOException e) {   
  39.    }   
  40.   }   
  41.   return address;   
  42.  }   
  43.   
  44.  // 通过IP获取网卡地址   
  45.  public static String getMacAddressIP(String remotePcIP) {   
  46.   String str = "";   
  47.   String macAddress = "";   
  48.   try {   
  49.    Process pp = Runtime.getRuntime().exec("nbtstat -A " + remotePcIP);   
  50.    InputStreamReader ir = new InputStreamReader(pp.getInputStream());   
  51.    LineNumberReader input = new LineNumberReader(ir);   
  52.    for (int i = 1; i < 100; i++) {   
  53.     str = input.readLine();   
  54.     if (str != null) {   
  55.      if (str.indexOf("MAC Address") > 1) {   
  56.       macAddress = str.substring(   
  57.         str.indexOf("MAC Address") + 14, str.length());   
  58.       break;   
  59.      }   
  60.     }   
  61.    }   
  62.   } catch (IOException ex) {   
  63.   }   
  64.   return macAddress;   
  65.  }   
  66.   
  67.  // 通过机器名获取网卡地址   
  68.  public static String getMacAddressName(String remotePcIP) {   
  69.   String str = "";   
  70.   String macAddress = "";   
  71.   try {   
  72.    Process pp = Runtime.getRuntime().exec("nbtstat -a " + remotePcIP);   
  73.    InputStreamReader ir = new InputStreamReader(pp.getInputStream());   
  74.    LineNumberReader input = new LineNumberReader(ir);   
  75.    for (int i = 1; i < 100; i++) {   
  76.     str = input.readLine();   
  77.     if (str != null) {   
  78.      if (str.indexOf("MAC Address") > 1) {   
  79.       macAddress = str.substring(   
  80.         str.indexOf("MAC Address") + 14, str.length());   
  81.       break;   
  82.      }   
  83.     }   
  84.    }   
  85.   } catch (IOException ex) {   
  86.   }   
  87.   return macAddress;   
  88.  }   
  89.   
  90.  public static void main(String[] args) {   
  91.   System.out.println(MACAddress.getMACAddress());            
  92.         System.out.println(getMacAddressIP("192.168.175.200"));          
  93.         System.out.println(getMacAddressName("527fefbedd5b43b."));   
  94.   
  95.  }   
  96. }   
  97.   
  98.   
  99.   
  100.   
  101.   
  102. package com.ctgusec.bean;   
  103.   
  104. import java.io.IOException;   
  105. import java.io.InputStream;   
  106. import java.io.InputStreamReader;   
  107. import java.io.LineNumberReader;   
  108.   
  109. /**  
  110.  *   
  111.  * @author zhupan  
  112.  * @version 1.0  
  113.  */  
  114.   
  115. public class GetMACAddress {   
  116.  public String getMACAddress(String ipAddress) {   
  117.   String str = "", strMAC = "", macAddress = "";   
  118.   try {   
  119.    Process pp = Runtime.getRuntime().exec("nbtstat -a " + ipAddress);   
  120.    InputStreamReader ir = new InputStreamReader(pp.getInputStream());   
  121.    LineNumberReader input = new LineNumberReader(ir);   
  122.    for (int i = 1; i < 100; i++) {   
  123.     str = input.readLine();   
  124.     if (str != null) {   
  125.      if (str.indexOf("MAC Address") > 1) {   
  126.       strMAC = str.substring(str.indexOf("MAC Address") + 14,   
  127.         str.length());   
  128.       break;   
  129.      }   
  130.     }   
  131.    }   
  132.   } catch (IOException ex) {   
  133.    return "Can't Get MAC Address!";   
  134.   }   
  135.   //   
  136.   if (strMAC.length() < 17) {   
  137.    return "Error!";   
  138.   }   
  139.   macAddress = strMAC.substring(02) + ":" + strMAC.substring(35)   
  140.     + ":" + strMAC.substring(68) + ":" + strMAC.substring(911)   
  141.     + ":" + strMAC.substring(1214) + ":"  
  142.     + strMAC.substring(1517);   
  143.   //   
  144.   return macAddress;   
  145.  }   
  146.   
  147.  public static void main(String[] args) {   
  148.   GetMACAddress getMACAddress = new GetMACAddress();   
  149.   System.out.println(getMACAddress.getMACAddress("192.168.175.66"));   
  150.   
  151.   try {   
  152.    java.lang.Process proc = Runtime.getRuntime().exec("ipconfig /all");   
  153.    InputStream istr = proc.getInputStream();   
  154.    byte[] data = new byte[1024];   
  155.    istr.read(data);   
  156.    String netdata = new String(data);   
  157.    System.out.println("Your Mac Address=" + procAll(netdata));   
  158.   } catch (IOException e) {   
  159.    System.out.println("error=" + e);   
  160.   }   
  161.  }   
  162.   
  163.  public static String procAll(String str) {   
  164.   return procStringEnd(procFirstMac(procAddress(str)));   
  165.  }   
  166.   
  167.  public static String procAddress(String str) {   
  168.   int indexof = str.indexOf("Physical Address");   
  169.   if (indexof > 0) {   
  170.    return str.substring(indexof, str.length());   
  171.   }   
  172.   return str;   
  173.  }   
  174.   
  175.  public static String procFirstMac(String str) {   
  176.   int indexof = str.indexOf(":");   
  177.   if (indexof > 0) {   
  178.    return str.substring(indexof + 1, str.length()).trim();   
  179.   }   
  180.   return str;   
  181.  }   
  182.   
  183.  public static String procStringEnd(String str) {   
  184.   int indexof = str.indexOf("\r");   
  185.   if (indexof > 0) {   
  186.    return str.substring(0, indexof).trim();   
  187.   }   
  188.   return str;   
  189.  }   
  190. }   
  191.   
  192.   
  193.     
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics