windows或linux系统下获取多网卡的ip地址

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

 

  
  
  
  
  1. public Vector<String> getServerIps() 
  2.    { 
  3.        Vector<String> address = new Vector<String>(); 
  4.        String linuxKey = "inet"
  5.        String window7Key = "IPv4"
  6.        String windowKey = "IP Address"
  7.        String os = System.getProperty("os.name"); 
  8.        if (os != null
  9.        { 
  10.            if (os.startsWith("Windows")) 
  11.            { 
  12.                try 
  13.                { 
  14.                    ProcessBuilder pb = new ProcessBuilder("ipconfig""/all"); 
  15.                    Process p = pb.start(); 
  16.                    BufferedReader br = new BufferedReader( 
  17.                        new InputStreamReader(p.getInputStream())); 
  18.                    String line; 
  19.                    while ((line = br.readLine()) != null
  20.                    { 
  21.                        if ((line.indexOf(window7Key) != -1
  22.                            || (line.indexOf(windowKey) != -1)) 
  23.                        { 
  24.                            int index = line.indexOf(":"); 
  25.                            int indexLast = line.indexOf("("); 
  26.                            String sbstr = null
  27.                            if (indexLast == -1
  28.                            { 
  29.                                sbstr = line.substring(index + 1).trim(); 
  30.  
  31.                            } 
  32.                            else 
  33.                            { 
  34.                                sbstr = line.substring(index + 1, indexLast).trim(); 
  35.  
  36.                            } 
  37.                            if (!sbstr.equals("127.0.0.1")) 
  38.                            { 
  39.                                address.add(sbstr); 
  40.                            } 
  41.                        } 
  42.                    } 
  43.                    br.close(); 
  44.                    return address; 
  45.                } 
  46.                catch (IOException e) 
  47.                { 
  48.                    String localIp = ""
  49.                    try 
  50.                    { 
  51.                        String localHost = InetAddress.getLocalHost().toString(); 
  52.                        localIp = localHost.split("/")[1]; 
  53.                    } 
  54.                    catch (UnknownHostException ex) 
  55.                    { 
  56.                        localIp = "127.0.0.1"
  57.                    } 
  58.                    address.add(localIp); 
  59.                } 
  60.            } 
  61.            else if (os.startsWith("Linux")) 
  62.            { 
  63.                try 
  64.                { 
  65.                    ProcessBuilder pb = new ProcessBuilder("ifconfig"); 
  66.                    Process p = pb.start(); 
  67.                    BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream())); 
  68.                    String line; 
  69.                    while ((line = br.readLine()) != null
  70.                    { 
  71.                        if (line.indexOf(linuxKey) != -1
  72.                        { 
  73.                            int index = line.indexOf(":"); 
  74.                            String sbstr = line.substring(index + 1).trim(); 
  75.                            if (!sbstr.equals("127.0.0.1")) 
  76.                            { 
  77.                                address.add(sbstr); 
  78.                            } 
  79.                        } 
  80.                    } 
  81.                    br.close(); 
  82.                    return address; 
  83.                } 
  84.                catch (IOException ex) 
  85.                { 
  86.                    String localIp = ""
  87.                    try 
  88.                    { 
  89.                        String localHost = InetAddress.getLocalHost().toString(); 
  90.                        localIp = localHost.split("/")[1]; 
  91.                    } 
  92.                    catch (UnknownHostException eu) 
  93.                    { 
  94.                        localIp = "127.0.0.1"
  95.                    } 
  96.                    address.add(localIp); 
  97.                } 
  98.  
  99.            } 
  100.        } 
  101.        return address; 
  102.    } 

你可能感兴趣的:(java)