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地址的方法的更多相关文章
- Java获取服务器IP和客户端IP
服务器IP: String addr = InetAddress.getLocalHost().getHostAddress(); 说明:很明显上面是没考虑到服务器有多个iP的情况. 客户顿啊IP: ...
- c#中如何获取本机用户名、MAC地址、IP地址、硬盘ID、CPU序列号、系统名称、物理内存
我们在利用C#开发桌面程序(Winform)程序的时候, 经常需要获取一些跟系统相关的信息, 以下这些代码获取能有些用处. c#中如何获取本机用户名.MAC地址.IP地址.硬盘ID.CPU序列号.系统 ...
- Python - 获取本机IP地址、Mac地址
Python - 获取本机IP地址.Mac地址 在python中获取ip地址和在php中有很大不同,在php中往往比较简单.那再python中怎么做呢? 直接看代码: # Python - 获取本机I ...
- 获取本机的IP地址和mac地址
1. 以前一直用ipconfig来查看ip地址,哈哈哈,现在发现挺好玩 #获取本机的IP地址和mac地址 import uuid import socket def get_mac_address() ...
- Java获取电脑IP、MAC、各种版本
Java代码获取电脑IP.MAC.各种版本 package com.rapoo.middle.action; import java.io.BufferedReader; import java.io ...
- 为什么同时需要IP地址和MAC地址
每个以太网设备在出厂时都有一个唯一的MAC地址,为什么还需要为每台主机再分配一个IP地址?或者说每台主机都分配唯一的IP地址,为什么还要在网络设备(如网卡,集线器,路由器等)生产时内嵌一个唯一的MAC ...
- 获取Android设备WIFI的MAC地址 “MAC地址”
需要指出的是:wifi状态和wifi AP状态是互斥的状态:也就是一旦发现WIFI AP打开,WIFI是不能被打开的. 获取Android设备的WIFI MAC地址,首先需要将设备中的WIFI个人热点 ...
- Linux环境下如何配置IP地址、MAC地址
Linux环境下如何配置IP地址.MAC地址 1.配置IP地址 进入配置IP地址路径,进行修改即可 cd /etc/network vim interface 加入以下内容: iface eth0 i ...
- 网络协议 2 - IP 地址和 MAC 地址
了解完网络协议,我们会发现,网络通信的五层模型里,有两个很重要的概念:IP 地址和 MAC 地址. 那么 IP 地址是怎么来的,又是怎么没的?MAC 地址与 IP 地址又有什么区别? 这回答上面问题前 ...
随机推荐
- Android中常见功能包描述(转)
在Android中,各种包写成android.*的方式,重要包的描述如下所示:android.app :提供高层的程序模型.提供基本的运行环境android.content:包含各种的对设备上的数据进 ...
- jQuery中bind方法和live方法区别解析
Javascript中的事件有它的独特性,有默认的执行事件,例如冒泡就是其中的一个.在很多比较复杂的应用程序中,停止事件的冒泡或捕获在程序开发当中是十分有用的,而在IE中有它的独特方式来阻止事件的冒泡 ...
- 如何清除SQL Server Management Studio的最近服务器列表
SQL Server Management Studio (SSMS) 的"连接到服务器"对话框会记录用户所有访问过的服务器名称,这个功能对于经常连接多个数据库的人来说确实挺方便的 ...
- Qt之qInstallMessageHandler(重定向至文件)
简述 在Qt之qInstallMessageHandler(输出详细日志)一节中,我们讲解了如何使用自定义消息处理程序输出调试信息,而且可以很直观.很方便的得到输出代码所在的文件.函数.行号等信息. ...
- 安卓微信浏览器中location.href失效的问题
在移动web中,经常会使用window.location.href去跳转页面,这个方法在绝大多数浏览器中都不会存在问题,但是在安卓手机的微信自带浏览器中,会出现一个奇怪的bug. window.loc ...
- 系统中异常公共处理模块 in spring boot
最近在用spring boot 做微服务,所以对于异常信息的 [友好展示]有要求,我设计了两点: 一. 在业务逻辑代码中,异常的抛出 我做了限定,一般只会是三种: 1. OmcException // ...
- 0-Spark高级数据分析-读书笔记
学完了<Spark快速大数据分析>,对Spark有了一些了解,计划更近一步,开始学习<Spark高级数据分析>.这本书是用Scala写的,在学习的过程中想把其中的代码转换成Ja ...
- iOS与JS交互实战篇(ObjC版)
前言 ObjectiveC与Js交互是常见的需求,可对于新手或者所谓的高手而言,其实并不是那么简单明了.这里只介绍iOS7.0后出来的JavaScriptCore framework. 关于JavaS ...
- ubuntu安装opencv
ubuntu版本是12.04 ,opencv的版本是2.4.9 其实官网有教程的,http://docs.opencv.org/doc/tutorials/introduction/linux_ins ...
- leetcode 187. Repeated DNA Sequences 求重复的DNA串 ---------- java
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...