[Java] Get the local IP address and the MAC address of the network card

Ordinary is just two words: laziness and laziness;
success is just two words: hardship and diligence;
excellence is just two words: you and me.
Follow me to learn JAVA, spring family bucket and linux operation and maintenance knowledge from 0, and take you from an ignorant teenager to the peak of life, and marry Bai Fumei!
Follow the WeChat public account [  IT is very reliable  ], and share technical experience every day~

 

Get the local IP address and the MAC address of the network card

1 What is a MAC address?

      MAC address (English: Media Access Control Address), literally translated as media access control address, also known as LAN address, MAC address, Ethernet address or physical address, it It is an address used to confirm the location of a network device. In the OSI model , the third network layer is responsible for IP addresses , and the second data link layer is responsible for MAC addresses. The MAC address is used to uniquely identify a network card in the network. If a device has one or more network cards, each network card has a unique MAC address!

 

2 Get IP address and MAC address

      Next, get the local ip address and network card MAC address through java.

2.1 Obtain an IP address

  /**
   * 获取本机IP地址
   */
  private static String getIpAddress() throws UnknownHostException {
    InetAddress ia = InetAddress.getLocalHost();
    return ia.getHostAddress();
  }

 

2.2 Get the MAC address of the network card

  /**
   * 获取本机使用网卡的MAC地址
   */
  private static String getMacAddress() throws UnknownHostException, SocketException {
    //获取IP地址,输出示例:WCGZ-DZ-013803/10.88.12.117
    InetAddress ia = InetAddress.getLocalHost();
    //获取网卡的MAC地址
    byte[] mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress();
    StringBuffer sb = new StringBuffer("");
    for (int i = 0; i < mac.length; i++) {
      if (i != 0) {
        sb.append("-");
      }
      //字节转换为整数
      int temp = mac[i] & 0xff;
      String str = Integer.toHexString(temp);
      if (str.length() == 1) {
        sb.append("0" + str);
      } else {
        sb.append(str);
      }
    }
    return sb.toString().toUpperCase();
  }

 

3 Complete code

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class Main {

  public static void main(String[] args) throws UnknownHostException, SocketException {
    //获取本机IP地址
    String ip = getIpAddress();
    //获取本机网卡的MAC地址
    String mac = getMacAddress();
    log.info("IP地址为:{}, 本机网卡MAC地址为:{}", ip, mac);
  }

  /**
   * 获取本机IP地址
   */
  private static String getIpAddress() throws UnknownHostException {
    InetAddress ia = InetAddress.getLocalHost();
    return ia.getHostAddress();
  }

  /**
   * 获取本机使用网卡的MAC地址
   */
  private static String getMacAddress() throws UnknownHostException, SocketException {
    //获取IP地址,输出示例:WCGZ-DZ-013803/10.88.12.117
    InetAddress ia = InetAddress.getLocalHost();
    //获取网卡的MAC地址
    byte[] mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress();
    StringBuffer sb = new StringBuffer("");
    for (int i = 0; i < mac.length; i++) {
      if (i != 0) {
        sb.append("-");
      }
      //字节转换为整数
      int temp = mac[i] & 0xff;
      String str = Integer.toHexString(temp);
      if (str.length() == 1) {
        sb.append("0" + str);
      } else {
        sb.append(str);
      }
    }
    return sb.toString().toUpperCase();
  }
}

 

4 Test results

      If you are helpful or need technical support, please pay attention to me~

 

Guess you like

Origin blog.csdn.net/IT_Most/article/details/109313606