jdk1.8 利用oshi-core-6.4.0.jar,通过IP、用户名、密码获取其他Linux服务器的CPU、内存、硬盘等详细信息(如要新增依赖,给出Maven坐标)
时间: 2023-12-14 16:05:38 浏览: 253
获取Linux内存、cpu、磁盘IO等信息
5星 · 资源好评率100%
可以使用Oshi库来获取Linux服务器的系统信息,它可以通过Maven依赖引入:
```xml
<dependency>
<groupId>com.github.oshi</groupId>
<artifactId>oshi-core</artifactId>
<version>6.4.0</version>
</dependency>
```
以下是示例代码,可以根据需要修改:
```java
import oshi.SystemInfo;
import oshi.hardware.CentralProcessor;
import oshi.hardware.GlobalMemory;
import oshi.hardware.HardwareAbstractionLayer;
import oshi.software.os.FileSystem;
import oshi.software.os.OSFileStore;
import oshi.software.os.OperatingSystem;
import java.util.Arrays;
import java.util.List;
public class ServerInfo {
public static void main(String[] args) {
String host = "192.168.1.100"; // 服务器IP地址
String user = "username"; // 登录用户名
String password = "password"; // 登录密码
try {
SSHManager sshManager = new SSHManager(host, user, password);
sshManager.connect();
// 获取CPU信息
SystemInfo systemInfo = new SystemInfo();
HardwareAbstractionLayer hardware = systemInfo.getHardware();
CentralProcessor processor = hardware.getProcessor();
System.out.println("CPU信息:");
System.out.println("处理器ID:" + processor.getProcessorID());
System.out.println("处理器名称:" + processor.getName());
System.out.println("处理器物理核心数:" + processor.getPhysicalProcessorCount());
System.out.println("处理器逻辑核心数:" + processor.getLogicalProcessorCount());
System.out.println("处理器负载:" + Arrays.toString(processor.getSystemLoadAverage(3)));
// 获取内存信息
GlobalMemory memory = hardware.getMemory();
System.out.println("内存信息:");
System.out.println("总内存:" + memory.getTotal());
System.out.println("可用内存:" + memory.getAvailable());
// 获取硬盘信息
FileSystem fileSystem = systemInfo.getOperatingSystem().getFileSystem();
List<OSFileStore> fileStores = Arrays.asList(fileSystem.getFileStores());
System.out.println("硬盘信息:");
for (OSFileStore fileStore : fileStores) {
System.out.println(fileStore.getName() + ":" + fileStore.getTotalSpace() + "/" + fileStore.getUsableSpace());
}
sshManager.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
请注意,此代码仅用于演示目的,实际应用中需要进行安全性、异常处理等方面的补充。需要自己实现SSH连接的管理,可以参考SSHManager类:
```java
import com.jcraft.jsch.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class SSHManager {
private String host;
private String user;
private String password;
private Session session;
private Channel channel;
private InputStream inputStream;
private OutputStream outputStream;
public SSHManager(String host, String user, String password) {
this.host = host;
this.user = user;
this.password = password;
}
public void connect() throws JSchException, IOException {
JSch jsch = new JSch();
session = jsch.getSession(user, host, 22);
session.setPassword(password);
session.setConfig("StrictHostKeyChecking", "no");
session.connect();
channel = session.openChannel("shell");
channel.connect();
inputStream = channel.getInputStream();
outputStream = channel.getOutputStream();
}
public void execute(String command) throws IOException {
outputStream.write(command.getBytes());
outputStream.flush();
}
public String read() throws IOException {
byte[] buffer = new byte[1024];
StringBuilder stringBuilder = new StringBuilder();
while (inputStream.available() > 0) {
int length = inputStream.read(buffer);
if (length > 0) {
stringBuilder.append(new String(buffer, 0, length));
}
}
return stringBuilder.toString();
}
public void close() {
try {
if (inputStream != null) {
inputStream.close();
}
if (outputStream != null) {
outputStream.close();
}
if (channel != null) {
channel.disconnect();
}
if (session != null) {
session.disconnect();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
阅读全文