博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java工具类,在Windows,Linux系统获取电脑的MAC地址、本地IP、电脑名
阅读量:7010 次
发布时间:2019-06-28

本文共 3784 字,大约阅读时间需要 12 分钟。

package com.cloudssaas.util;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.net.InetAddress;import java.util.ArrayList;import java.util.Iterator;import java.util.List;import java.util.regex.Matcher;import java.util.regex.Pattern;/********************************************************************************* * //* Copyright (C) 2014 ××××××××××. All Rights Reserved. //* //* Filename: * ComputerInfo.java //* Revision: 1.0 //* Author: 
//* Created On: * 2014年5月21日 //* Modified by: //* Modified On: //* //* Description: * *
<取网卡物理地址-- * 1.在windows,linux系统下均可用; 2.通过ipconifg,ifconfig获得计算机信息; 3.再用模式匹配方式查找mac地址,与操作系统的语言无关>
* * //* Description:
<取计算机名--从环境变量中取>
* abstract 限制继承/创建实例 *//********************************************************************************/public abstract class ComputerInfo { private static String macAddressStr = null; private static String computerName = System.getenv().get("COMPUTERNAME"); private static final String[] windowsCommand = { "ipconfig", "/all" }; private static final String[] linuxCommand = { "/sbin/ifconfig", "-a" }; private static final Pattern macPattern = Pattern.compile(".*((:?[0-9a-f]{2}[-:]){5}[0-9a-f]{2}).*", Pattern.CASE_INSENSITIVE); /** * 获取多个网卡地址 * * @return * @throws IOException */ private final static List
getMacAddressList() throws IOException { final ArrayList
macAddressList = new ArrayList
(); final String os = System.getProperty("os.name"); final String command[]; if (os.startsWith("Windows")) { command = windowsCommand; } else if (os.startsWith("Linux")) { command = linuxCommand; } else { throw new IOException("Unknow operating system:" + os); } // 执行命令 final Process process = Runtime.getRuntime().exec(command); BufferedReader bufReader = new BufferedReader(new InputStreamReader(process.getInputStream())); for (String line = null; (line = bufReader.readLine()) != null;) { Matcher matcher = macPattern.matcher(line); if (matcher.matches()) { macAddressList.add(matcher.group(1)); // macAddressList.add(matcher.group(1).replaceAll("[-:]", // ""));//去掉MAC中的“-” } } process.destroy(); bufReader.close(); return macAddressList; } /** * 获取一个网卡地址(多个网卡时从中获取一个) * * @return */ public static String getMacAddress() { if (macAddressStr == null || macAddressStr.equals("")) { StringBuffer sb = new StringBuffer(); // 存放多个网卡地址用,目前只取一个非0000000000E0隧道的值 try { List
macList = getMacAddressList(); for (Iterator
iter = macList.iterator(); iter.hasNext();) { String amac = iter.next(); if (!amac.equals("0000000000E0")) { sb.append(amac); break; } } } catch (IOException e) { e.printStackTrace(); } macAddressStr = sb.toString(); } return macAddressStr; } /** * 获取电脑名 * * @return */ public static String getComputerName() { if (computerName == null || computerName.equals("")) { computerName = System.getenv().get("COMPUTERNAME"); } return computerName; } /** * 获取客户端IP地址 * * @return */ public static String getIpAddrAndName() throws IOException { return InetAddress.getLocalHost().toString(); } /** * 获取客户端IP地址 * * @return */ public static String getIpAddr() throws IOException { return InetAddress.getLocalHost().getHostAddress().toString(); } /** * 获取电脑唯一标识 * * @return */ public static String getComputerID() { String id = getMacAddress(); if (id == null || id.equals("")) { try { id = getIpAddrAndName(); } catch (IOException e) { e.printStackTrace(); } } return computerName; } /** * 限制创建实例 */ private ComputerInfo() { } public static void main(String[] args) throws IOException { System.out.println(ComputerInfo.getMacAddress()); System.out.println(ComputerInfo.getComputerName()); System.out.println(ComputerInfo.getIpAddr()); System.out.println(ComputerInfo.getIpAddrAndName()); }}

 

相关:

转载地址:http://kxttl.baihongyu.com/

你可能感兴趣的文章
我的友情链接
查看>>
隔离公司各个部门--虚拟路由器(EBGP)
查看>>
postfix队列里邮件超时退信设置
查看>>
【博客话题】发肿是嫉妒的表现!谁像死人一样发肿!
查看>>
SSD数据可靠性问题分析
查看>>
RHEL(RedHat Enterprise Linux)5/6 ISO 镜像[转]
查看>>
Linux意外之rpm的删除与恢复
查看>>
SVN使用教程
查看>>
对vc++类和对象的逆向研究
查看>>
OD消息断点的设置方法
查看>>
nagios 监控 网卡流量 脚本
查看>>
物理机向虚拟机迁移P2V
查看>>
我的友情链接
查看>>
CentOS在ssh下远程重装系统
查看>>
nginx变量使用方法详解(1)
查看>>
<!DOCTYPE html>声明下div高度100%的问题
查看>>
踢出登录用户
查看>>
Linux下apache安全配置策略
查看>>
我的友情链接
查看>>
2014年PHP框架前十排行榜
查看>>