Java获取本机名称、网卡名称、IP、MAC

获取所有IP

 /**
     * 获取该主机上所有网卡的ip
     */
    public static ArrayList<String> getAllHostIp(){
        ArrayList<String> hostAddress = new ArrayList<>();
        try{
            Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
            while (allNetInterfaces.hasMoreElements()){
                NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
                Enumeration<InetAddress> addresses = netInterface.getInetAddresses();
                while (addresses.hasMoreElements()){
                    InetAddress ip = (InetAddress) addresses.nextElement();
                    if (ip instanceof Inet4Address
                            && !ip.isLoopbackAddress() //loopback地址即本机地址,IPv4的loopback范围是127.0.0.0 ~ 127.255.255.255
                            && !ip.getHostAddress().contains(":")){
                        hostAddress.add(ip.getHostAddress());
                    }
                }
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return hostAddress;
    }

 结果:

[192.168.239.1, 192.168.41.1, 192.168.2.169, 192.168.7.200, 192.168.6.200]

获取本机名、IP、MAC地址

import java.net.InetAddress;
import java.net.NetworkInterface;
 
public class IpConfig {
	@SuppressWarnings("static-access")
	public static void main(String[] args) throws Exception {
		InetAddress ia = null;
		try {
			ia = ia.getLocalHost();
			String localname = ia.getHostName();
			String localip = ia.getHostAddress();
			System.out.println("本机名称是:" + localname);
			System.out.println("本机的ip是 :" + localip);
		} catch (Exception e) {
			e.printStackTrace();
		}
		InetAddress ia1 = InetAddress.getLocalHost();// 获取本地IP对象
		System.out.println("本机的MAC是 :" + getMACAddress(ia1));
	}
 
	// 获取MAC地址的方法
	private static String getMACAddress(InetAddress ia) throws Exception {
		// 获得网络接口对象(即网卡),并得到mac地址,mac地址存在于一个byte数组中。
		byte[] mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress();
		// 下面代码是把mac地址拼装成String
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < mac.length; i++) {
			if (i != 0) {
				sb.append("-");
			}
			// mac[i] & 0xFF 是为了把byte转化为正整数
			String s = Integer.toHexString(mac[i] & 0xFF);
			// System.out.println("--------------");
			// System.err.println(s);
			sb.append(s.length() == 1 ? 0 + s : s);
		}
		// 把字符串所有小写字母改为大写成为正规的mac地址并返回
		return sb.toString().toUpperCase();
	}
}

结果:

本机名称是:PC-DaiHaijiao
本机的ip是 :172.16.0.31

本机的MAC是 :00-FF-0D-99-5E-1E

 获取网卡名和IP

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;
 
public class NetworkInterfaceTest {
 
	public static void main(String[] args) throws Exception {
		// 获得本机的所有网络接口
		Enumeration<NetworkInterface> nifs = NetworkInterface.getNetworkInterfaces();
		while (nifs.hasMoreElements()) {
			NetworkInterface nif = nifs.nextElement();
			// 获得与该网络接口绑定的 IP 地址,一般只有一个
			Enumeration<InetAddress> addresses = nif.getInetAddresses();
			while (addresses.hasMoreElements()) {
				InetAddress addr = addresses.nextElement();
				if (addr instanceof Inet4Address) { // 只关心 IPv4 地址
					System.out.println("网卡接口名称:" + nif.getName());
					System.out.println("网卡接口地址:" + addr.getHostAddress());
					System.out.println();
				}
			}
		}
	}
}

结果:

网卡接口名称:lo
网卡接口地址:127.0.0.1
 

网卡接口名称:eth0
网卡接口地址:172.168.1.100

网卡接口名称:eth1
网卡接口地址:172.168.1.200

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

今晚哒老虎

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

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

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

打赏作者

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

抵扣说明:

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

余额充值