这篇文章主要介绍了java编程实现获取机器IP地址及MAC地址的方法,实例分析了Java分别针对单网卡及多网卡的情况下获取服务器IP地址与MAC地址的相关技巧,需要的朋友可以参考下
 

本文实例讲述了java编程实现获取服务器IP地址及MAC地址的方法。分享给大家供大家参考,具体如下:

已测系统:
windows linux unix

排除127.0.0.1 和 0.0.0.0.1等非正常IP

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
public class IpUtil {
private IpUtil(){}
/**
* 此方法描述的是:获得服务器的IP地址
* @author: zhangyang33@sinopharm.com
* @version: 2014年9月5日 下午4:57:15
*/
public static String getLocalIP() {
String sIP = "";
InetAddress ip = null;
try {
boolean bFindIP = false;
Enumeration<NetworkInterface> netInterfaces = (Enumeration<NetworkInterface>) NetworkInterface
.getNetworkInterfaces();
while (netInterfaces.hasMoreElements()) {
if (bFindIP) {
break;
}
NetworkInterface ni = (NetworkInterface) netInterfaces
.nextElement();
Enumeration<InetAddress> ips = ni.getInetAddresses();
while (ips.hasMoreElements()) {
ip = (InetAddress) ips.nextElement();
if (!ip.isLoopbackAddress()
&& ip.getHostAddress().matches(
"(\\d{1,3}\\.){3}\\d{1,3}")) {
bFindIP = true;
break;
}
}
}
} catch (Exception e) {
OutUtil.error(IpUtil.class, e.getMessage());
}
if (null != ip) {
sIP = ip.getHostAddress();
}
return sIP;
}
/**
* 此方法描述的是:获得服务器的IP地址(多网卡)
* @author: zhangyang33@sinopharm.com
* @version: 2014年9月5日 下午4:57:15
*/
public static List<String> getLocalIPS() {
InetAddress ip = null;
List<String> ipList = new ArrayList<String>();
try {
Enumeration<NetworkInterface> netInterfaces = (Enumeration<NetworkInterface>) NetworkInterface
.getNetworkInterfaces();
while (netInterfaces.hasMoreElements()) {
NetworkInterface ni = (NetworkInterface) netInterfaces
.nextElement();
Enumeration<InetAddress> ips = ni.getInetAddresses();
while (ips.hasMoreElements()) {
ip = (InetAddress) ips.nextElement();
if (!ip.isLoopbackAddress()
&& ip.getHostAddress().matches(
"(\\d{1,3}\\.){3}\\d{1,3}")) {
ipList.add(ip.getHostAddress());
}
}
}
} catch (Exception e) {
OutUtil.error(IpUtil.class, e.getMessage());
}
return ipList;
}
/**
* 此方法描述的是:获得服务器的MAC地址
* @author: zhangyang33@sinopharm.com
* @version: 2014年9月5日 下午1:27:25
*/
public static String getMacId() {
String macId = "";
InetAddress ip = null;
NetworkInterface ni = null;
try {
boolean bFindIP = false;
Enumeration<NetworkInterface> netInterfaces = (Enumeration<NetworkInterface>) NetworkInterface
.getNetworkInterfaces();
while (netInterfaces.hasMoreElements()) {
if (bFindIP) {
break;
}
ni = (NetworkInterface) netInterfaces
.nextElement();
// ----------特定情况,可以考虑用ni.getName判断
// 遍历所有ip
Enumeration<InetAddress> ips = ni.getInetAddresses();
while (ips.hasMoreElements()) {
ip = (InetAddress) ips.nextElement();
if (!ip.isLoopbackAddress() // 非127.0.0.1
&& ip.getHostAddress().matches(
"(\\d{1,3}\\.){3}\\d{1,3}")) {
bFindIP = true;
break;
}
}
}
} catch (Exception e) {
OutUtil.error(IpUtil.class, e.getMessage());
}
if (null != ip) {
try {
macId = getMacFromBytes(ni.getHardwareAddress());
} catch (SocketException e) {
OutUtil.error(IpUtil.class, e.getMessage());
}
}
return macId;
}
/**
* 此方法描述的是:获得服务器的MAC地址(多网卡)
* @author: zhangyang33@sinopharm.com
* @version: 2014年9月5日 下午1:27:25
*/
public static List<String> getMacIds() {
InetAddress ip = null;
NetworkInterface ni = null;
List<String> macList = new ArrayList<String>();
try {
Enumeration<NetworkInterface> netInterfaces = (Enumeration<NetworkInterface>) NetworkInterface
.getNetworkInterfaces();
while (netInterfaces.hasMoreElements()) {
ni = (NetworkInterface) netInterfaces
.nextElement();
// ----------特定情况,可以考虑用ni.getName判断
// 遍历所有ip
Enumeration<InetAddress> ips = ni.getInetAddresses();
while (ips.hasMoreElements()) {
ip = (InetAddress) ips.nextElement();
if (!ip.isLoopbackAddress() // 非127.0.0.1
&& ip.getHostAddress().matches(
"(\\d{1,3}\\.){3}\\d{1,3}")) {
macList.add(getMacFromBytes(ni.getHardwareAddress()));
}
}
}
} catch (Exception e) {
OutUtil.error(IpUtil.class, e.getMessage());
}
return macList;
}
private static String getMacFromBytes(byte[] bytes) {
StringBuffer mac = new StringBuffer();
byte currentByte;
boolean first = false;
for (byte b : bytes) {
if (first) {
mac.append("-");
}
currentByte = (byte) ((b & 240) >> 4);
mac.append(Integer.toHexString(currentByte));
currentByte = (byte) (b & 15);
mac.append(Integer.toHexString(currentByte));
first = true;
}
return mac.toString().toUpperCase();
}

java获取服务器IP地址及MAC地址的方法的更多相关文章

  1. Java获取服务器IP和客户端IP

    服务器IP: String addr = InetAddress.getLocalHost().getHostAddress(); 说明:很明显上面是没考虑到服务器有多个iP的情况. 客户顿啊IP: ...

  2. c#中如何获取本机用户名、MAC地址、IP地址、硬盘ID、CPU序列号、系统名称、物理内存

    我们在利用C#开发桌面程序(Winform)程序的时候, 经常需要获取一些跟系统相关的信息, 以下这些代码获取能有些用处. c#中如何获取本机用户名.MAC地址.IP地址.硬盘ID.CPU序列号.系统 ...

  3. Python - 获取本机IP地址、Mac地址

    Python - 获取本机IP地址.Mac地址 在python中获取ip地址和在php中有很大不同,在php中往往比较简单.那再python中怎么做呢? 直接看代码: # Python - 获取本机I ...

  4. 获取本机的IP地址和mac地址

    1. 以前一直用ipconfig来查看ip地址,哈哈哈,现在发现挺好玩 #获取本机的IP地址和mac地址 import uuid import socket def get_mac_address() ...

  5. Java获取电脑IP、MAC、各种版本

    Java代码获取电脑IP.MAC.各种版本 package com.rapoo.middle.action; import java.io.BufferedReader; import java.io ...

  6. 为什么同时需要IP地址和MAC地址

    每个以太网设备在出厂时都有一个唯一的MAC地址,为什么还需要为每台主机再分配一个IP地址?或者说每台主机都分配唯一的IP地址,为什么还要在网络设备(如网卡,集线器,路由器等)生产时内嵌一个唯一的MAC ...

  7. 获取Android设备WIFI的MAC地址 “MAC地址”

    需要指出的是:wifi状态和wifi AP状态是互斥的状态:也就是一旦发现WIFI AP打开,WIFI是不能被打开的. 获取Android设备的WIFI MAC地址,首先需要将设备中的WIFI个人热点 ...

  8. Linux环境下如何配置IP地址、MAC地址

    Linux环境下如何配置IP地址.MAC地址 1.配置IP地址 进入配置IP地址路径,进行修改即可 cd /etc/network vim interface 加入以下内容: iface eth0 i ...

  9. 网络协议 2 - IP 地址和 MAC 地址

    了解完网络协议,我们会发现,网络通信的五层模型里,有两个很重要的概念:IP 地址和 MAC 地址. 那么 IP 地址是怎么来的,又是怎么没的?MAC 地址与 IP 地址又有什么区别? 这回答上面问题前 ...

随机推荐

  1. gulp基本用法

    嗨,小伙伴们,大家周五好,又到了一周中最最最期待的周五啦啦~~~ 这几天一直在研究gulp的使用方法,今天抽时间来整理一下基本步骤. gulp 的使用流程: 安装nodejs ->安装git(方 ...

  2. 关于Java中的基本数据类型转换

    Java中的基本类型有四种,其中整型分为byte.short.int.long,浮点型分为float.double,字符型char,布尔型boolean.8种类型的级别由低到高byte->sho ...

  3. Sql/Plus连接Oracle时候出现sql*net not properly installed 解决办法

    在PLSQL Developer选择Tools > Preferences > options > 下的如图所示:"Oracle Home" and " ...

  4. this其实是js的一个对象谁调用它它就指向谁

    本人看了一下,感觉对this解释的有点复杂了,因此,本人在此给this一个简单易于理解的定义. 因为上面计算出来的结果不符合我们的习惯,并且负值在计算的时候会影响正确性,现在我们给这个结果加上180 ...

  5. VS 2005 修复重置(深度重置)

    /resetuserdata 参数 如果 Visual Studio 在运行时被损坏,且无法从损坏状态进行恢复,您可以使用此参数将 Visual Studio 重置到其使用之初的状态.这些问题的例子可 ...

  6. 数位DP之奥义

    恩是的没错数位DP的奥义就是一个简练的dfs模板 int dfs(int position, int condition, bool boundary) { ) return (condition ? ...

  7. 【OpenGL】 第一篇 OpenGL概览

    ---------------------------------------------------------------------------------------------------- ...

  8. [转]使用Maven添加依赖项时(Add Dependency)时,没有提示项目可用,并且在Console中,输出: Unable to update index for central|http://repo1.maven.org/maven2 。

    使用Maven添加依赖项时(Add Dependency)时,没有提示项目可用,并且在Console中,输出: Unable to update index for central|http://re ...

  9. maven2 com.jhlabs.imaging 01012005 maven安装jar包imaging命令

    com.jhlabs:imaging:jar:01012005 所在仓库+captcha验证码maven依赖 maven 安装jar包 到本地仓库 命令maven 3.0安装jar包 到本地仓库 co ...

  10. JAVA语言的本质优势

    虽然Java应用最广泛,但与其它语言比并没有什么技术上的优势.常会看到各种抱怨,说Java语言设计不如C#,对native的精确控制和灵活性不然C++,动态性.开发效率方便不如Ruby,Python, ...