java如何获取Linux操作系统下的ip地址和网卡信息? window下的我现在已经得到,可是linux下的尚未得到,请高手指点! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 ip地址用: InetAddress ipaddr = InetAddress.getLocalHost(); System.out.println( "Localhost ip: "+ipaddr.getHostAddress()); 网卡信息用 ProcessBuilder pb = new ProcessBuilder("ifconfig"); 然后处理下 上面写的是window下可以用,linux下可以吗 以下代码需要 JDK 6 及以后版本,如果是 JDK 6 以下版本的话,没有直接获得 MAC 的 API。import java.net.InetAddress;import java.net.NetworkInterface;import java.net.SocketException;import java.util.Enumeration;public class NetworkInfo { private final static char[] HEX = "0123456789ABCDEF".toCharArray(); public static void main(String[] args) throws SocketException { for(Enumeration<NetworkInterface> i = NetworkInterface.getNetworkInterfaces(); i.hasMoreElements(); ) { NetworkInterface ni = i.nextElement(); System.out.println("NETWORK CARD NAME: " + ni.getDisplayName()); System.out.println("MAC: " + toMacString(ni.getHardwareAddress())); for(Enumeration<InetAddress> j = ni.getInetAddresses(); j.hasMoreElements(); ) { System.out.println(" " + j.nextElement()); } } } private static String toMacString(byte[] bys) { if(bys == null) { return null; } char[] chs = new char[bys.length * 3 - 1]; for(int i = 0, k = 0; i < bys.length; i++) { if(i > 0) { chs[k++] = '-'; } chs[k++] = HEX[(bys[i] >> 4) & 0xf]; chs[k++] = HEX[bys[i] & 0xf]; } return new String(chs); }} package com.xuz.csdn.june19;import java.net.InetAddress;import java.net.NetworkInterface;import java.net.SocketException;import java.net.UnknownHostException;public class NetworkInterfaceTest { public static void main(String[] args) { try { InetAddress address = InetAddress.getLocalHost(); NetworkInterface netCard = NetworkInterface .getByInetAddress(address); byte[] addr = netCard.getHardwareAddress(); StringBuffer sb = new StringBuffer(); for (int i = 0; i < addr.length; i++) { if (addr[i] != 0) { sb.append("-"); } String string = Integer.toHexString(addr[i] & 0xff); sb.append(string.length() == 1 ? "0" + string : string); } System.out.println(sb.toString().toUpperCase()); } catch (SocketException e) { e.printStackTrace(); } catch (UnknownHostException e) { e.printStackTrace(); }// Enumeration<NetworkInterface> interfaces = null;// try {// interfaces = NetworkInterface.getNetworkInterfaces();// } catch (SocketException e) {// e.printStackTrace();// }// while (interfaces.hasMoreElements()) {// final NetworkInterface ni = interfaces.nextElement();// try {// if (ni.isLoopback() || ni.isPointToPoint() || ni.isVirtual())// continue;// } catch (SocketException e) {// e.printStackTrace();// }// byte[] macAddress = null;// try {// macAddress = ni.getHardwareAddress();// } catch (SocketException e) {// e.printStackTrace();// }// if (macAddress != null && macAddress.length > 0)// try {// System.out.println(new String(macAddress,"gbk"));// } catch (UnsupportedEncodingException e) {// e.printStackTrace();// }// } }}昨天学习的。 在iunx下还可以取得其他的信息吗,例如硬盘的物理序列号,cup的序列号 火龙果: 你昨天给我发过来的代码,运行结果是这样的NETWORK CARD NAME: eth0MAC: 00-E0-4C-0D-BA-5E /fe80:0:0:0:2e0:4cff:fe0d:ba5e%2 /192.168.8.105NETWORK CARD NAME: loMAC: null /0:0:0:0:0:0:0:1%1 /127.0.0.1就得不到别的信息了吗? 双网卡的话 那样获取不到吧.应该是调用linux系统 从返回值中获取你要的信息 /** * 取当前系统站点本地地址 linux下 和 window下可用 add by RWW * * @return */ public static String getLocalIP() { String sIP = ""; InetAddress ip = null; try { // 如果是Windows操作系统 if (isWindowsOS()) { ip = InetAddress.getLocalHost(); } // 如果是Linux操作系统 else { boolean bFindIP = false; Enumeration<NetworkInterface> netInterfaces = (Enumeration<NetworkInterface>) NetworkInterface .getNetworkInterfaces(); while (netInterfaces.hasMoreElements()) { if (bFindIP) { break; } NetworkInterface ni = (NetworkInterface) netInterfaces.nextElement(); // ----------特定情况,可以考虑用ni.getName判断 // 遍历所有ip Enumeration<InetAddress> ips = ni.getInetAddresses(); while (ips.hasMoreElements()) { ip = (InetAddress) ips.nextElement(); if (ip.isSiteLocalAddress() && !ip.isLoopbackAddress() // 127.开头的都是lookback地址 && ip.getHostAddress().indexOf(":") == -1) { bFindIP = true; break; } } } } } catch (Exception e) { e.printStackTrace(); MyLogger.logserverLogger.error("MultiThreadedTrapReceiver 453: " + e.getMessage()); } if (null != ip) { sIP = ip.getHostAddress(); } return sIP; } 不行就Runtime.getRuntime.exex嘛。 /** * 判断当前系统是否windows * * @return */ public static boolean isWindowsOS() { boolean isWindowsOS = false; String osName = System.getProperty("os.name"); if (osName.toLowerCase().indexOf("windows") > -1) { isWindowsOS = true; } return isWindowsOS; } java的路,迷茫中,大虾们讨论下吧 急****电子邮件javax.mail.AuthenticationFailedException: EOF on socket 我上传错了资料,怎么删除啊? JXL 如何跨Sheet 设置公式 如何在服务器上面实施部署 tomcat6.0 连接池配置 请有经验的人指点一下maxIdle、maxWait、maxActive的值设置 jboss在LINUX下的问题!高手快帮我啊! 请问应该如何调用??? 测试BMP EJB时客户端出现以下错误 求助长数字到短数字的映射算法 下载文件不用response 还可以用什么做? 网站首页如何显示文章和内容简介?
ip地址用:
InetAddress ipaddr = InetAddress.getLocalHost();
System.out.println( "Localhost ip: "+ipaddr.getHostAddress());
网卡信息用
ProcessBuilder pb = new ProcessBuilder("ifconfig");
然后处理下
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;public class NetworkInfo { private final static char[] HEX = "0123456789ABCDEF".toCharArray(); public static void main(String[] args) throws SocketException {
for(Enumeration<NetworkInterface> i = NetworkInterface.getNetworkInterfaces(); i.hasMoreElements(); ) {
NetworkInterface ni = i.nextElement();
System.out.println("NETWORK CARD NAME: " + ni.getDisplayName());
System.out.println("MAC: " + toMacString(ni.getHardwareAddress()));
for(Enumeration<InetAddress> j = ni.getInetAddresses(); j.hasMoreElements(); ) {
System.out.println(" " + j.nextElement());
}
}
} private static String toMacString(byte[] bys) {
if(bys == null) {
return null;
}
char[] chs = new char[bys.length * 3 - 1];
for(int i = 0, k = 0; i < bys.length; i++) {
if(i > 0) {
chs[k++] = '-';
}
chs[k++] = HEX[(bys[i] >> 4) & 0xf];
chs[k++] = HEX[bys[i] & 0xf];
}
return new String(chs);
}
}
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;public class NetworkInterfaceTest { public static void main(String[] args) { try {
InetAddress address = InetAddress.getLocalHost();
NetworkInterface netCard = NetworkInterface
.getByInetAddress(address);
byte[] addr = netCard.getHardwareAddress(); StringBuffer sb = new StringBuffer();
for (int i = 0; i < addr.length; i++) {
if (addr[i] != 0) {
sb.append("-");
} String string = Integer.toHexString(addr[i] & 0xff);
sb.append(string.length() == 1 ? "0" + string : string);
} System.out.println(sb.toString().toUpperCase());
} catch (SocketException e) {
e.printStackTrace();
} catch (UnknownHostException e) {
e.printStackTrace();
}// Enumeration<NetworkInterface> interfaces = null;
// try {
// interfaces = NetworkInterface.getNetworkInterfaces();
// } catch (SocketException e) {
// e.printStackTrace();
// }
// while (interfaces.hasMoreElements()) {
// final NetworkInterface ni = interfaces.nextElement();
// try {
// if (ni.isLoopback() || ni.isPointToPoint() || ni.isVirtual())
// continue;
// } catch (SocketException e) {
// e.printStackTrace();
// }
// byte[] macAddress = null;
// try {
// macAddress = ni.getHardwareAddress();
// } catch (SocketException e) {
// e.printStackTrace();
// }
// if (macAddress != null && macAddress.length > 0)
// try {
// System.out.println(new String(macAddress,"gbk"));
// } catch (UnsupportedEncodingException e) {
// e.printStackTrace();
// }
// } }}
昨天学习的。
你昨天给我发过来的代码,运行结果是这样的
NETWORK CARD NAME: eth0
MAC: 00-E0-4C-0D-BA-5E
/fe80:0:0:0:2e0:4cff:fe0d:ba5e%2
/192.168.8.105
NETWORK CARD NAME: lo
MAC: null
/0:0:0:0:0:0:0:1%1
/127.0.0.1
就得不到别的信息了吗?
应该是调用linux系统 从返回值中获取你要的信息
* 取当前系统站点本地地址 linux下 和 window下可用 add by RWW
*
* @return
*/
public static String getLocalIP()
{
String sIP = "";
InetAddress ip = null;
try
{
// 如果是Windows操作系统
if (isWindowsOS())
{
ip = InetAddress.getLocalHost();
}
// 如果是Linux操作系统
else
{
boolean bFindIP = false;
Enumeration<NetworkInterface> netInterfaces = (Enumeration<NetworkInterface>) NetworkInterface
.getNetworkInterfaces();
while (netInterfaces.hasMoreElements())
{
if (bFindIP)
{
break;
}
NetworkInterface ni = (NetworkInterface) netInterfaces.nextElement();
// ----------特定情况,可以考虑用ni.getName判断
// 遍历所有ip
Enumeration<InetAddress> ips = ni.getInetAddresses();
while (ips.hasMoreElements())
{
ip = (InetAddress) ips.nextElement();
if (ip.isSiteLocalAddress() && !ip.isLoopbackAddress() // 127.开头的都是lookback地址
&& ip.getHostAddress().indexOf(":") == -1)
{
bFindIP = true;
break;
}
}
}
}
}
catch (Exception e)
{
e.printStackTrace();
MyLogger.logserverLogger.error("MultiThreadedTrapReceiver 453: " + e.getMessage());
}
if (null != ip)
{
sIP = ip.getHostAddress();
}
return sIP;
}
* 判断当前系统是否windows
*
* @return
*/
public static boolean isWindowsOS()
{
boolean isWindowsOS = false;
String osName = System.getProperty("os.name");
if (osName.toLowerCase().indexOf("windows") > -1)
{
isWindowsOS = true;
}
return isWindowsOS;
}