Java获取多台服务器CPU、内存和硬盘信息实现方式
作者:卜旭凯
本篇文章详细介绍了使用Java获取多台服务器CPU、内存和硬盘信息的方法,通过导入特定依赖库实现自动化监控,只需修改IP信息即可轻松运行,适合IT运维人员参考
Java获取多台服务器CPU、内存和硬盘信息
POM中导入依赖库
<!--JSch 依赖库-->
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.55</version>
</dependency>其他复制粘贴
服务器ip信息改成自己的,直接运行即可
package xxx.xxx.xxx.utils;
import com.jcraft.jsch.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
/**
* @ClassName: ServerInfoUtil
* @Description: 服务器信息工具类
* @author: boxukai
* @date:2025年04月15日 14:27:08
*/
public class ServerInfoUtil {
// 服务器列表,包含 IP、用户名和密码
private static List<Server> servers = new ArrayList<>();
static {
// 假设 2 台服务器的信息
servers.add(new Server("你的服务ip1", "你的服务账号", "你的服务密码"));
servers.add(new Server("你的服务ip2", "你的服务账号", "你的服务密码"));
// 依次添加其他服务器的信息
}
// 内部类表示服务器信息
static class Server {
String ip;
String username;
String password;
public Server(String ip, String username, String password) {
this.ip = ip;
this.username = username;
this.password = password;
}
}
// 执行 SSH 命令并获取输出
private static String executeCommand(String ip, String username, String password, String command) {
StringBuilder output = new StringBuilder();
JSch jsch = new JSch();
Session session = null;
try {
session = jsch.getSession(username, ip, 22);
session.setPassword(password);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
Channel channel = session.openChannel("exec");
((ChannelExec) channel).setCommand(command);
channel.setInputStream(null);
((ChannelExec) channel).setErrStream(System.err);
InputStream in = channel.getInputStream();
channel.connect();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line;
while ((line = reader.readLine()) != null) {
output.append(line).append("\n");
}
channel.disconnect();
session.disconnect();
} catch (JSchException | IOException e) {
e.printStackTrace();
}
return output.toString();
}
// 获取内存信息
private static void getMemoryInfo() {
for (Server server : servers) {
String command = "free -h";
String result = executeCommand(server.ip, server.username, server.password, command);
System.out.println("Server: " + server.ip + "\nMemory Info:\n" + result);
}
}
// 获取 CPU 信息
private static void getCPUInfo() {
for (Server server : servers) {
String command = "lscpu";
String result = executeCommand(server.ip, server.username, server.password, command);
System.out.println("Server: " + server.ip + "\nCPU Info:\n" + result);
}
}
// 获取磁盘信息
private static void getDiskInfo() {
for (Server server : servers) {
String command = "df -h";
String result = executeCommand(server.ip, server.username, server.password, command);
System.out.println("Server: " + server.ip + "\nDisk Info:\n" + result);
}
}
public static void main(String[] args) {
getMemoryInfo();
getCPUInfo();
getDiskInfo();
}
}总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
