连接linux服务器执行命令

  1. 导入jsch依赖
<dependency>
    <groupId>com.jcraft</groupId>
    <artifactId>jsch</artifactId>
    <version>0.1.55</version>
</dependency>
  1. 代码块
import com.jcraft.jsch.*;

import java.io.IOException;
import java.io.InputStream;

public class RemoteServerMonitor {
    private static JSch jsch;
    private static Session session;

    /**
     * 创建连接
     * @param host
     * @param port
     * @param username
     * @param password
     * @throws JSchException
     */
    private static void connect(String host, int port, String username, String password) throws JSchException {
        if (jsch == null) {
            jsch = new JSch();
            session = jsch.getSession(username, host, port);
            session.setPassword(password);
            session.setConfig("StrictHostKeyChecking", "no");
            session.connect();
        }
    }

    /**
     * 关闭连接
     */
    private static void disconnect() {
        session.disconnect();
    }

    /**
     * 运行命令获取结果
     * @param command
     * @return
     * @throws JSchException
     * @throws IOException
     */
    private static String executeCommand(String command) throws JSchException, IOException {
        ChannelExec channel = (ChannelExec) session.openChannel("exec");
        channel.setCommand(command);
        channel.setInputStream(null);
        channel.setErrStream(System.err);

        InputStream in = channel.getInputStream();
        channel.connect();

        StringBuilder output = new StringBuilder();
        byte[] buffer = new byte[1024];
        int bytesRead;

        while ((bytesRead = in.read(buffer)) != -1) {
            output.append(new String(buffer, 0, bytesRead));
        }

        channel.disconnect();
        return output.toString();
    }

    private static double parseCpuUsage(String commandOutput) {
        String[] lines = commandOutput.split("\n");
        String cpuLine = lines[0];
        String[] cpuInfo = cpuLine.split(", ");
        String cpuUsage = cpuInfo[0].split(": ")[1].trim().split(" ")[0];

        return Double.parseDouble(cpuUsage);
    }

    /**
     * 获取cpu使用率
     * @return
     */
    private static double getRemoteCpuUsage() {
        try {
            //获取cpu使用率命令
            String command = "top -b -n 1 | grep Cpu";
            String commandOutput = executeCommand(command);
            double cpuUsage = parseCpuUsage(commandOutput);

            return cpuUsage;
        } catch (JSchException | IOException e) {
            e.printStackTrace();
            return -1;
        }
    }

    public static void main(String[] args) {
        try {
            connect("clickhouse-virtual-machine", 22, "clickhouse", "root");
            for (int i = 0; i < 10; i++) {
                Thread.sleep(1000);
                double cpu = getRemoteCpuUsage();
                System.out.println("cpu:" + cpu );
            }
        } catch (JSchException | InterruptedException e) {
            e.printStackTrace();
        }

        disconnect();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值