public static String getIPByShell(String inetType) { String ifip = ""; StringBuilder sb = new StringBuilder(); String command = "/usr/sbin/ifconfig " + inetType; try { Process p = Runtime.getRuntime().exec(command); BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream())); String line = null; while ((line = br.readLine()) != null) { sb.append(line); } br.close(); p.destroy(); } catch (IOException e) { e.printStackTrace(); } String ifinfo = sb.toString(); if (ifinfo.indexOf("netmask") == -1) { logger.warn("command=[{}] 没有返回 [netmask]相关的信息", command); } else { // 从netmask处截取,然后以空格拆分,最后一个值即为内网IP String[] ifinfoArr = ifinfo.substring(0, ifinfo.indexOf("netmask")).split(" "); ifip = ifinfoArr[ifinfoArr.length - 1]; } return ifip; }
/** * 判断操作系统是否 win 或者 mac 等个人电脑 */ public static boolean isPC(){ String os = System.getProperty("os.name"); if(os.toLowerCase().startsWith("win") || os.toLowerCase().startsWith("mac")){ return true; } return false; }
原文:https://www.cnblogs.com/wanhua-wu/p/13601263.html