java擷取本機名稱、IP、MAC地址和網卡名稱

來源:互聯網
上載者:User

標籤:content   amp   col   not   cal   ipv4   get   imp   代碼   

java擷取本機名稱、IP、MAC地址和網卡名稱

摘自:80364370 

2018年05月18日 14:53:19閱讀數:134

  
  1. import java.net.InetAddress;
  2. import java.net.NetworkInterface;
  3. public class IpConfig {
  4. @SuppressWarnings("static-access")
  5. public static void main(String[] args) throws Exception {
  6. InetAddress ia = null;
  7. try {
  8. ia = ia.getLocalHost();
  9. String localname = ia.getHostName();
  10. String localip = ia.getHostAddress();
  11. System.out.println("本機名稱是:" + localname);
  12. System.out.println("原生ip是 :" + localip);
  13. } catch (Exception e) {
  14. e.printStackTrace();
  15. }
  16. InetAddress ia1 = InetAddress.getLocalHost();// 擷取本地IP對象
  17. System.out.println("原生MAC是 :" + getMACAddress(ia1));
  18. }
  19. // 擷取MAC地址的方法
  20. private static String getMACAddress(InetAddress ia) throws Exception {
  21. // 獲得網路介面對象(即網卡),並得到mac地址,mac地址存在於一個byte數組中。
  22. byte[] mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress();
  23. // 下面代碼是把mac地址拼裝成String
  24. StringBuffer sb = new StringBuffer();
  25. for (int i = 0; i < mac.length; i++) {
  26. if (i != 0) {
  27. sb.append("-");
  28. }
  29. // mac[i] & 0xFF 是為了把byte轉化為正整數
  30. String s = Integer.toHexString(mac[i] & 0xFF);
  31. // System.out.println("--------------");
  32. // System.err.println(s);
  33. sb.append(s.length() == 1 ? 0 + s : s);
  34. }
  35. // 把字串所有小寫字母改為大寫成為正規的mac地址並返回
  36. return sb.toString().toUpperCase();
  37. }
  38. }

輸出結果如下:

本機名稱是:PC-DaiHaijiao
原生ip是 :172.16.0.31

原生MAC是 :00-FF-0D-99-5E-1E



  
  1. import java.net.Inet4Address;
  2. import java.net.InetAddress;
  3. import java.net.NetworkInterface;
  4. import java.util.Enumeration;
  5. public class NetworkInterfaceTest {
  6. public static void main(String[] args) throws Exception {
  7. // 獲得原生所有網路介面
  8. Enumeration<NetworkInterface> nifs = NetworkInterface.getNetworkInterfaces();
  9. while (nifs.hasMoreElements()) {
  10. NetworkInterface nif = nifs.nextElement();
  11. // 獲得與該網路介面綁定的 IP 位址,一般只有一個
  12. Enumeration<InetAddress> addresses = nif.getInetAddresses();
  13. while (addresses.hasMoreElements()) {
  14. InetAddress addr = addresses.nextElement();
  15. if (addr instanceof Inet4Address) { // 只關心 IPv4 地址
  16. System.out.println("網卡介面名稱:" + nif.getName());
  17. System.out.println("網卡介面地址:" + addr.getHostAddress());
  18. System.out.println();
  19. }
  20. }
  21. }
  22. }
  23. }

輸出結果如下:

網卡介面名稱:lo
網卡介面地址:127.0.0.1


網卡介面名稱:eth0
網卡介面地址:172.16.0.31


網卡介面名稱:eth2
網卡介面地址:192.168.220.1


網卡介面名稱:wlan2
網卡介面地址:192.168.0.108


網卡介面名稱:eth8
網卡介面地址:192.168.138.1

java擷取本機名稱、IP、MAC地址和網卡名稱

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.