在获取CPU型号时,input.readLine())始终为null 后来改为shell命令获取能获取到
public static String getCpuModle() throws Exception { // 获取CPU型号 List<String> infoList = new ArrayList<String>();// Process process = Runtime.getRuntime().exec(// "cat /proc/cpuinfo | grep name | cut -f2 -d: | uniq -c"); String[] cmd = new String[] { "/bin/sh", "-c", "cat /proc/cpuinfo | grep name | cut -f2 -d: | uniq -c" }; Process process = Runtime.getRuntime().exec(cmd); //2 Intel(R) Xeon(R) CPU E5645 @ 2.40GHz process.getOutputStream().close();// Scanner sc = new Scanner(process.getInputStream());// while (sc.hasNext()) { // infoList.add(sc.next().split(":")[1]);// }// String[] infoArr = new String[infoList.size()];// for (int i = 0; i < infoList.size(); i++) { // infoArr[i] = infoList.get(i);// }// // return infoArr; BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream())); String cpuInfo = ""; String line = null; while((line = input.readLine()) != null) { cpuInfo += line; } System.out.println(cpuInfo); return cpuInfo; }
2.获取内存信息
在获取内存信息时,使用scanner的sc.next()方法获取数据时,
完整数据时MemTotal: 3869040 kB
但是只是扫描到MemTotal:
后面的数值因为前面有空格停止扫描,所以没有读取出来
public static String getMemoryInfo() throws Exception {
// 获取MemoryInfo Process process = Runtime.getRuntime().exec( "grep MemTotal /proc/meminfo"); process.getOutputStream().close(); BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream())); String totalMemory = ""; String line = null; while((line = input.readLine().trim()) != null) { totalMemory += line; } System.out.println(totalMemory); return totalMemory; }
3.获取硬盘大小
这个没问题 public static String getDistInfo() throws Exception { String rootPath = System.getProperty("user.dir"); String home = "/home"; if (rootPath.startsWith("/opt")) { home = "/opt"; } File homeFile = new File(home); long totalSapce = homeFile.getTotalSpace() / (1024 * 1024 * 1024); return String.valueOf(totalSapce); }
4.获取MAC地址
在获取MACAdress时,使用InetAddress ip = InetAddress.getLocalHost();获取不到网卡信息 linux配了localhost的hosts映射,用localhost方法估计取不到网卡信息.用虚拟机真实IP地址取能行public static String getMACAddress() throws Exception {
//InetAddress ip = InetAddress.getLocalHost(); NetworkInterface network = NetworkInterface.getByInetAddress(ip); StringBuilder sb = new StringBuilder(); if (network != null) { byte[] mac = network.getHardwareAddress(); if (mac != null) { for (int i = 0; i < mac.length; i++) { sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? ":" : "")); } } } return sb.toString(); }
5.获取linux的真是ip地址
InetAddress inet = InetAddress.getLocalHost(); System.out.println("本机的ip=" + inet.getHostAddress()); 之前使用InetAddress.getLocalHost(); 在Linux下返回127.0.0.1。主要是在linux下返回的是/etc/hosts中配置的localhost的ip地址,而不是网卡的绑定地址,
public static String getIpAddress() throws Exception {
String ip = ""; try { for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) { NetworkInterface intf = en.nextElement(); String name = intf.getName(); if (!name.contains("docker") && !name.contains("lo")) { for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) { InetAddress inetAddress = enumIpAddr.nextElement(); if (!inetAddress.isLoopbackAddress()) { String ipaddress = inetAddress.getHostAddress().toString(); if (!ipaddress.contains("::") && !ipaddress.contains("0:0:") && !ipaddress.contains("fe80")) { ip = ipaddress; // System.out.println(ipaddress); } } } } } } catch (Exception ex) { System.out.println("获取ip地址异常"); ip = "127.0.0.1"; ex.printStackTrace(); } //System.out.println("IP:"+ip); return ip; }