你好,游客 登录 注册 搜索
背景:
阅读新闻

Windows和Linux系统下获取多网卡的ip地址

[日期:2013-04-01] 来源:Linux社区  作者:Slive [字体:大 中 小]

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

 public Vector<String> getServerIps()
    {
        Vector<String> address = new Vector<String>();
        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;
    }

linux
相关资讯       Windows和Linux 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款