设为首页 加入收藏

TOP

Windows和Linux系统下获取多网卡的ip地址
2014-11-24 07:40:27 来源: 作者: 【 】 浏览:0
Tags:Windows Linux 系统 获取 网卡 地址

在Windows或者Linux操作系统中,获取多网卡信息,可通过执行命令方式获取,具体如下:


public Vector getServerIps()
{
Vector address = new Vector();
String linuxKey = "inet";
String window7Key = "IPv4";
String windowKey = "IP Address";
String os = System.getProperty("os.name");
if (os != null)
{
if (os.startsWith("Windows"))
{
try
{
ProcessBuilder pb = new ProcessBuilder("ipconfig", "/all");
Process p = pb.start();
BufferedReader br = new BufferedReader(
new InputStreamReader(p.getInputStream()));
String line;
while ((line = br.readLine()) != null)
{
if ((line.indexOf(window7Key) != -1)
|| (line.indexOf(windowKey) != -1))
{
int index = line.indexOf(":");
int indexLast = line.indexOf("(");
String sbstr = null;
if (indexLast == -1)
{
sbstr = line.substring(index + 1).trim();


}
else
{
sbstr = line.substring(index + 1, indexLast).trim();


}
if (!sbstr.equals("127.0.0.1"))
{
address.add(sbstr);
}
}
}
br.close();
return address;
}
catch (IOException e)
{
String localIp = "";
try
{
String localHost = InetAddress.getLocalHost().toString();
localIp = localHost.split("/")[1];
}
catch (UnknownHostException ex)
{
localIp = "127.0.0.1";
}
address.add(localIp);
}
}
else if (os.startsWith("Linux"))
{
try
{
ProcessBuilder pb = new ProcessBuilder("ifconfig");
Process p = pb.start();
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = br.readLine()) != null)
{
if (line.indexOf(linuxKey) != -1)
{
int index = line.indexOf(":");
String sbstr = line.substring(index + 1).trim();
if (!sbstr.equals("127.0.0.1"))
{
address.add(sbstr);
}
}
}
br.close();
return address;
}
catch (IOException ex)
{
String localIp = "";
try
{
String localHost = InetAddress.getLocalHost().toString();
localIp = localHost.split("/")[1];
}
catch (UnknownHostException eu)
{
localIp = "127.0.0.1";
}
address.add(localIp);
}


}
}
return address;
}


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇慎用Java递归调用 下一篇Linux内核线程死锁或死循环之后如..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Java 并发工具类:提 (2025-12-25 20:25:44)
·Java面试技巧:如何 (2025-12-25 20:25:41)
·Java并发编程中的线 (2025-12-25 20:25:38)
·C 语言 - cppreferen (2025-12-25 19:50:27)
·《C 语言入门教程》 (2025-12-25 19:50:23)