获得主机名和MAC地址

[url=http://www.blogjava.net/nokiaguy/archive/2009/04/14/265404.html]使用getCanonicalHostName方法获得主机名[/url]

[url=http://www.cnblogs.com/hxsyl/p/3422191.html]Java获取本机MAC地址[/url]
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;

/*
* 物理地址是48位,别和ipv6搞错了
*/
public class LOCALMAC {
/**
* @param args
* @throws UnknownHostException
* @throws SocketException
*/
public static void main(String[] args) throws UnknownHostException,
SocketException {
// TODO Auto-generated method stub

// 得到IP,输出PC-201309011313/122.206.73.83
InetAddress ia = InetAddress.getLocalHost();
System.out.println(ia);
getLocalMac(ia);
}

private static void getLocalMac(InetAddress ia) throws SocketException {
// TODO Auto-generated method stub
// 获取网卡,获取地址
byte[] mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress();
System.out.println("mac数组长度:" + mac.length);
StringBuffer sb = new StringBuffer("");
for (int i = 0; i < mac.length; i++) {
if (i != 0) {
sb.append("-");
}
// 字节转换为整数
int temp = mac[i] & 0xff;
String str = Integer.toHexString(temp);
System.out.println("每8位:" + str);
if (str.length() == 1) {
sb.append("0" + str);
} else {
sb.append(str);
}
}
System.out.println("本机MAC地址:" + sb.toString().toUpperCase());
}
}


[url=http://hngmduyi.iteye.com/blog/1860084]java 获取MAC地址[/url]
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;


/**
* @title: MacAddressUtil
* @description:获取MAC地址
* @author:liming
* @date: 2013-5-5 下午04:42:50
*/
public class MacAddressUtil{

/**
* @MethodName: getOSName
* @description : 获取当前操作系统名称. return 操作系统名称 例如:windows,Linux,Unix等
* @author:liming
* @date: 2013-5-5 下午04:43:36
* @return String
*/
public static String getOSName() {
return System.getProperty("os.name").toLowerCase();
}

/**
* @MethodName: getWindowXPMACAddress
* @description : 获取widnowXp网卡的mac地址
* @author:liming
* @date: 2013-5-5 下午04:45:12
* @param execStr
* @return String
*/
public static String getWindowXPMACAddress(String execStr) {
String mac = null;
BufferedReader bufferedReader = null;
Process process = null;
try {
// windows下的命令,显示信息中包含有mac地址信息
process = Runtime.getRuntime().exec(execStr);
bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
int index = -1;
while ((line = bufferedReader.readLine()) != null) {
if(line.indexOf("本地连接") != -1) //排除有虚拟网卡的情况
continue;

// 寻找标示字符串[physical address]
index = line.toLowerCase().indexOf("physical address");
if (index != -1) {
index = line.indexOf(":");
if (index != -1) {
//取出mac地址并去除2边空格
mac = line.substring(index + 1).trim();
}
break;
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bufferedReader != null) {
bufferedReader.close();
}
} catch (IOException e1) {
e1.printStackTrace();
}
bufferedReader = null;
process = null;
}
return mac;
}

/**
* @MethodName: getWindow7MACAddress
* @description : 获取widnow7网卡的mac地址
* @author:liming
* @date: 2013-5-5 下午04:46:56
* @param execStr
* @return String
*/
public static String getWindow7MACAddress() {
//获取本地IP对象
InetAddress ia = null;
try {
ia = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
e.printStackTrace();
}
//获得网络接口对象(即网卡),并得到mac地址,mac地址存在于一个byte数组中。
byte[] mac = null;
try {
mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress();
} catch (SocketException e) {
e.printStackTrace();
}
//下面代码是把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);
sb.append(s.length()==1?0+s:s);
}
//把字符串所有小写字母改为大写成为正规的mac地址并返回
return sb.toString().toUpperCase();
}

/**
* @MethodName: getLinuxMACAddress
* @description : 获取Linux网卡的mac地址
* @author:liming
* @date: 2013-5-5 下午04:49:13
* @return String
*/
public static String getLinuxMACAddress() {
String mac = null;
BufferedReader bufferedReader = null;
Process process = null;
try {
// linux下的命令,一般取eth0作为本地主网卡 显示信息中包含有mac地址信息
process = Runtime.getRuntime().exec("ifconfig eth0");
bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
int index = -1;
while ((line = bufferedReader.readLine()) != null) {
index = line.toLowerCase().indexOf("硬件地址");
if (index != -1) {
// 取出mac地址并去除2边空格
mac = line.substring(index + 4).trim();
break;
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bufferedReader != null) {
bufferedReader.close();
}
} catch (IOException e1) {
e1.printStackTrace();
}
bufferedReader = null;
process = null;
}
return mac;
}

/**
* @MethodName: getUnixMACAddress
* @description : 获取Unix网卡的mac地址
* @author:liming
* @date: 2013-5-5 下午04:49:59
* @return String
*/
public static String getUnixMACAddress() {
String mac = null;
BufferedReader bufferedReader = null;
Process process = null;
try {
// Unix下的命令,一般取eth0作为本地主网卡 显示信息中包含有mac地址信息
process = Runtime.getRuntime().exec("ifconfig eth0");
bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
int index = -1;
while ((line = bufferedReader.readLine()) != null) {
// 寻找标示字符串[hwaddr]
index = line.toLowerCase().indexOf("hwaddr");
if (index != -1) {
// 取出mac地址并去除2边空格
mac = line.substring(index + "hwaddr".length() + 1).trim();
break;
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bufferedReader != null) {
bufferedReader.close();
}
} catch (IOException e1) {
e1.printStackTrace();
}
bufferedReader = null;
process = null;
}

return mac;
}

/**
* @MethodName: getMacAddress
* @description :获取MAC地址
* @author:liming
* @date: 2013-5-5 下午04:53:25
* @return String
*/
public static String getMacAddress(){
String os = getOSName();
String execStr = getSystemRoot()+"/system32/ipconfig /all";
String mac = null;
if(os.startsWith("windows")){
if(os.equals("windows xp")){//xp
mac = getWindowXPMACAddress(execStr);
}else if(os.equals("windows 2003")){//2003
mac = getWindowXPMACAddress(execStr);
}else{//win7
mac = getWindow7MACAddress();
}
}else if (os.startsWith("linux")) {
mac = getLinuxMACAddress();
}else{
mac = getUnixMACAddress();
}
return mac;
}

/**
* @MethodName: getSystemRoot
* @description :jdk1.4获取系统命令路径 :SystemRoot=C:\WINDOWS
* @author:liming
* @date: 2013-5-5 下午04:56:27
* @return String
*/
public static String getSystemRoot(){
String cmd = null;
String os = null;
String result = null;
String envName = "windir";
os = System.getProperty("os.name").toLowerCase();
if(os.startsWith("windows")) {
cmd = "cmd /c SET";
}else {
cmd = "env";
}
try {
Process p = Runtime.getRuntime().exec(cmd);
InputStreamReader isr = new InputStreamReader(p.getInputStream());
BufferedReader commandResult = new BufferedReader(isr);
String line = null;
while ((line = commandResult.readLine()) != null) {
line=line.toLowerCase();//重要(同一操作系统不同电脑有些返回的是小写,有些是大写.因此需要统一转换成小写)
if (line.indexOf(envName) > -1) {
result = line.substring(line.indexOf(envName)
+ envName.length() + 1);
return result;
}
}
} catch (Exception e) {
System.out.println("获取系统命令路径 error: " + cmd + ":" + e);
}
return null;
}


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值