博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
获取linux硬件信息
阅读量:5150 次
发布时间:2019-06-13

本文共 3644 字,大约阅读时间需要 12 分钟。

在获取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;
        }

转载于:https://www.cnblogs.com/buyx/p/6836992.html

你可能感兴趣的文章
Pycharm Error loading package list:Status: 403错误解决方法
查看>>
steps/train_sat.sh
查看>>
TLS 1.0协议
查看>>
java递归的几种用法
查看>>
转:Linux设备树(Device Tree)机制
查看>>
iOS 组件化
查看>>
python安装win32api pywin32 后出现 ImportError: DLL load failed
查看>>
(转)Tomcat 8 安装和配置、优化
查看>>
(转)Linxu磁盘体系知识介绍及磁盘介绍
查看>>
tkinter布局
查看>>
命令ord
查看>>
利用新浪微博来控制电脑
查看>>
洛谷 P3367 【模板】并查集
查看>>
方法Equals和操作符==的区别
查看>>
我的软件工程师之路,给需要的同学!
查看>>
快速模幂
查看>>
Unity3D_最简单的开始界面_结束界面
查看>>
TCP/IP五层模型
查看>>
Sharepoint 2013搜索服务配置总结(实战)
查看>>
10 个用来下载免费图标的网站
查看>>