日期:2014-05-19 浏览次数:21013 次
public static void main(String[] args) throws IOException { String os = System.getProperties().getProperty("os.name"); //得到操作系统 xp 为"Windows XP" 其他的的楼主自己去试试 Process pro = Runtime.getRuntime().exec("ipconfig"); BufferedReader br = new BufferedReader(new InputStreamReader(pro.getInputStream())); List<String> rowList = new ArrayList(); String temp; while((temp = br.readLine()) != null){ rowList.add(temp ); } for (String string : rowList) { String sm= os.equals("Windows XP") ? "Subnet Mask" : "子网掩码" ; //这里只判断了win7个xp if(string.indexOf("Subnet Mask") != -1){ Matcher mc = Pattern.compile("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}").matcher(string); if(mc.find()){ System.out.println("子掩码:" + mc.group()); }else{ System.out.println("子掩码为空"); } }; String dg = os.equals("Windows XP") ? "Default Gateway" : "默认网关" ; //这里只判断了win7个xp if(string.indexOf("Default Gateway") != -1){ Matcher mc = Pattern.compile("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}").matcher(string); if(mc.find()){ System.out.println("默认网关:" + mc.group()); }else{ System.out.println("默认网关为空"); } return; }; } }
------解决方案--------------------
/** * 获取linuxShell命令结果. * @param cmd 指令 * @return 指令结果 */ public String getRuntimeExec(final String cmd) { BufferedReader bufferedReader = null; Process process = null; StringBuffer sb = new StringBuffer(); try { String buffer; //System.out.println(cmd); process = Runtime.getRuntime().exec(cmd); // 得到结果 bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream())); while ((buffer = bufferedReader.readLine()) != null) { sb.append(buffer + "\n"); } if (sb.length() > 0) { sb.deleteCharAt(sb.length() - 1); } return sb.toString(); } catch (IOException e) { // TODO Auto-generated catch block System.out.println("execute failed."); return null; } finally { // 清理 try { if (bufferedReader != null) { bufferedReader.close(); bufferedReader = null; } if (process != null) { process.destroy(); process = null; } } catch (Exception e) { e.printStackTrace(); } } }
------解决方案--------------------
也来弄一段:,只能得到掩码
public static void main(String args[]) { try { for (Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInte