利用jdk1.6 获取服务器ip和mac
来源:互联网 发布:win7 网络连接 感叹号 编辑:程序博客网 时间:2024/06/07 08:12
刚开始最初的想法是调用系统命令来获取,但是这样的话需要考虑不同的操作系统的差异,不同操作系统执行的命令是不一样的。
1. 获取操作系统
System.getProperty("os.name").toLowerCase();
可能取值:win7 --> windows 7 ; windows ,其他
2. windows 获取ip 和mac 执行命令ipconfig/all
Process process = Runtime.getRuntime().exec("ipconfig /all");
InputStreamReader ir = new InputStreamReader(process.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
String line;
while ((line = input.readLine()) != null) {//mac地址
if (line.indexOf("Physical Address") > 0) {
String MACAddr = line.substring(line.indexOf("-") - 2);
System.out.println("MAC address = [" + MACAddr + "]");
}
if (line.indexOf(" IP Address") > 0) {//ip地址
String MACAddr = line.substring(line.indexOf("-") - 2);
System.out.println("MAC address = [" + MACAddr + "]");
}
但是此种方法对于win7无法获取
对于linux process = Runtime.getRuntime().exec("ifconfig bond0");
但是有一个问题,可能我们一台机器上不止一个网卡,有可能有虚拟的网卡,这样子的话用执行命令就不好处理
而jdk1.6其实自带了api来查询ip和mac
public static String[] getIpAddr() {
try {
Enumeration allNetInterfaces = null;
try {
allNetInterfaces = NetworkInterface.getNetworkInterfaces();
} catch (SocketException e) {
e.printStackTrace();
}
InetAddress ip = null;
byte[] mac;
while (allNetInterfaces.hasMoreElements()) {
NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
// System.out.println(netInterface.getName());
Enumeration addresses = netInterface.getInetAddresses();
while (addresses.hasMoreElements()) {
ip = (InetAddress) addresses.nextElement();
if (ip != null && ip.isSiteLocalAddress() && !ip.isLoopbackAddress()
&& ip.getHostAddress().indexOf(":") == -1)
if (ip != null && ip instanceof Inet4Address && ip.getHostAddress().indexOf(".") != -1) {
mac = netInterface.getHardwareAddress();
String ipStr = ip.getHostAddress();
String macStr = changeMac(mac);
// System.out.println(ipStr);
// System.out.println(macStr);
String result[] = new String[2];
result[0] = ipStr;
result[1] = macStr;
return result;
}
}
}
} catch (Exception e) {
return null;
}
// }
return null;
}
- 利用jdk1.6 获取服务器ip和mac
- 总结 asp.net 和 javascript获取本地IP(MAC)和服务器IP(MAC)的方法
- 获取服务器的IP地址和MAC地址
- JDK1.6 获取操作系统IP地址及对应物理mac地址
- action获取IP和MAC
- VS获取mac和ip
- java 获取IP和MAC
- C#获取本机相关mac和ip信息以及服务器ip
- 获取服务器IP和获取访问者IP
- 利用NetworkInterface获取服务器MAC地址
- 利用SDF2.3获取Windows Mobile上的IP地址和MAC地址
- 获取服务器ip,本地ip,本在mac
- java获取本地IP和服务器IP
- 利用pypthon得到IP和MAC地址
- javaWeb快速获取服务器的IP和对应的Mac地址
- linux 利用ioctl函数获取IP地址、子网掩码、MAC地址和修改本机IP地址、子网掩码、网关
- ZZ-linux中获取IP和MAC
- 获取MAC地址和IP地址
- InvokeRequired 属性 与Invoke方法
- Linux Epoll介绍和程序实例
- 【设计模式】在NBA需要翻译 --- 适配器模式
- 【开源】完美破解九宫格(数独)游戏
- kthread_run 学习
- 利用jdk1.6 获取服务器ip和mac
- bin目录下的Release和Debug与obj目录下的Release和Debug的区别
- C# 中文数据导出CSV出现乱码解决方法
- 黑马程序员--Java学习日记10_交通灯系统
- 软件-eclipse
- BUGS
- 清除BOM头
- 后PC时代科技投资新思路
- Linux File Permission