package sterning.queue;
import java.net.InetAddress;
import java.io.InputStream;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.text.ParseException;
import java.util.StringTokenizer;
public final class NetworkUtils {
private final static int MACADDR_LENGTH = 17;
private final static String WIN_OSNAME = "Windows";
private final static String WIN_MACADDR_REG_EXP =
"^[0-9A-F]{2}-[0-9A-F]{2}-[0-9A-F]{2}-[0-9A-F]{2}-[0-9A-F]{2}-[0-9A-F]{2}$";
private final static String WIN_MACADDR_EXEC = "ipconfig /all";
public final static String getMacAddress() throws IOException {
String os = System.getProperty("os.name");
try {
if (os.startsWith(WIN_OSNAME)) {
return winMacAddress(winIpConfigCommand());
}
// 下面是其它的操作系统的代码,省略了!
// LINUX --> else if (os.startsWith("Linux")) {
//
// Process p = Runtime.getRuntime().exec("ifconfig");
// MAC OSX --> else if(os.startsWith("Mac OS X")) {
//
// Process p = Runtime.getRuntime().exec("ifconfig");
else {
throw new IOException("OS not supported : " + os);
}
}
catch(ParseException e) {
e.printStackTrace();
throw new IOException(e.getMessage());
}
}
private final static String winMacAddress(String ipConfigOutput)
throws ParseException {
String localHost = null;
try {
localHost = InetAddress.getLocalHost().getHostAddress();
}
catch(java.net.UnknownHostException ex) {
ex.printStackTrace();
throw new ParseException(ex.getMessage(), 0);
}
StringTokenizer tokenizer = new StringTokenizer(ipConfigOutput, "\n");
String lastMacAddress = null;
while(tokenizer.hasMoreTokens()) {
String line = tokenizer.nextToken().trim();
// see if line contains IP address
if (line.endsWith(localHost) && lastMacAddress != null) {
return lastMacAddress;
}
// see if line contains MAC address
int macAddressPosition = line.indexOf(":");
if(macAddressPosition <= 0) continue;
String macAddressCandidate = line.substring(macAddressPosition + 1).trim();
if (winIsMacAddress(macAddressCandidate)) {
lastMacAddress = macAddressCandidate;
continue;
}
}
ParseException ex = new ParseException
("cannot read MAC address from [" + ipConfigOutput + "]", 0);
ex.printStackTrace();
throw ex;
}
private final static boolean winIsMacAddress(String macAddressCandidate) {
if (macAddressCandidate.length() != MACADDR_LENGTH) return false;
if (!macAddressCandidate.matches(WIN_MACADDR_REG_EXP)) return false;
return true;
}
private final static String winIpConfigCommand() throws IOException {
Process p = Runtime.getRuntime().exec(WIN_MACADDR_EXEC);
InputStream stdoutStream = new BufferedInputStream(p.getInputStream());
StringBuffer buffer= new StringBuffer();
for (;;) {
int c = stdoutStream.read();
if (c == -1) break;
buffer.append((char)c);
}
String outputText = buffer.toString();
stdoutStream.close();
return outputText;
}
public final static void main(String[] args) {
try {
System.out.println("MAC ADDRESS");
System.out.println(" OS : "
+ System.getProperty("os.name"));
System.out.println(" IP/Localhost: "
+ InetAddress.getLocalHost().getHostAddress());
System.out.println(" MAC Address : "
+ getMacAddress());
}
catch(Throwable t) {
t.printStackTrace();
}
}
}
分享到:
相关推荐
刚刚发了一个获取电脑硬盘序列号的demo 想想还是把这个获取MAC地址的也发上来吧 请大家指点一二
在对网内主机进行监控时需要通过java获取远程机器的mac地址
java获取本机网卡的MAC地址,/** * 测试用的main方法. * * @param argc * 运行参数. */ public static void main(String[] argc) { String os = getOSName(); System.out.println(os); if(os....
主要介绍了java编程实现获取机器IP地址及MAC地址的方法,实例分析了Java分别针对单网卡及多网卡的情况下获取服务器IP地址与MAC地址的相关技巧,需要的朋友可以参考下
Java/JSP获得客户端网卡MAC地址的三种方法解析
java获取Centos7服务器网卡ip 子网掩码 默认网关 DNS 同时设置网卡 及 重启网卡参考
//Java获得CPU序列号和网卡Mac地址 /* 利用Runtime call操作系统的命令,具体的命令取决于不同的操作系统,注意不要调用Runtime.getRuntime().exec(String)接口,要用Runtime.getRuntime().exec(String[])这个接口,...
NULL 博文链接:https://hylong05.iteye.com/blog/560600
很容易的就可以获得机器的MAC(网卡)地址,是用java语言编写的
java 程序, 亲测。 获取计算机的网卡IP地址。 runttime 的调用。
允许这个测试类,就可以轻松的获取到本地连接的mac网卡地址了,就可以作为设备的唯一编码。
C#程序源码,自己写的小工具,用于获取本机Mac地址,并对Mac地址进行AES加密及解密,已经过测试,方便使用。
很容易就可以获得 机器的MAC地址,我这使用java编写的。
java.io.FileInputStream fis = new java.io.FileInputStream( new java.io.File("d:\\数据库.zip")); // 转换成网络输出流 java.net.ServerSocket ss = new java.net.ServerSocket(9000); java.net....
网上搜集了获取IP、MAC方法做了总结,提供多种获取IP、mac方式。支持获取多网卡IP、MAc获取
10.JAVA获取系统当前的用户 7 11.javascript获取客户端网卡MAC地址和IP地址和计算机名 7 12.JAVA反射 8 13. 自定义ClassLoader 13 14. double保留两位数的两个方案 18 16. 用户输入输出,文件输入输出范例(1) 19 17....
Wifi连接网络,通过程序你将了解到定义WifiManager对象,定义WifiInfo对象,获取网络连接列表,定义一个WifiLock,打开Wifi网卡,关闭Wifi网卡,检查当前Wifi网卡状态,扫描周边网络,每次点击扫描之前清空上一次的...
一个Android连接WiFi设置的例子,可以显示WiFi列表,设置连接指定的WIFI,查看已保存wifi的密码(需要root权限),连接上wifi的详细信息:本机IP,本机MAC,网关,DNS,网卡接口名等信息。 之前上传过的DEMO:后来...
lib 官方jar包以及底层支撑文件问题解答1、mac下eclipse中配置java.library.pathMac下,eclipse中的配置方法:项目右键->属性->java build path->选择Libraries中的sigar.jar->在展开的Native library location中...
Java版水果管理系统源码 “后台开发”指的是“服务端的网 络程序开 发”,从功能上可以具体描述为 :服务器收到客户端发来的 请求数据,解析请求数据后处理,最后返回结果 计算机网络 输入网址后发生了什么 1.浏览器...