java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > java跳板执行ssh命令

java跳板执行ssh命令方式

作者:princeAladdin

本文分享了在Java中使用跳板机执行SSH命令的方法,并推荐了一些Maven依赖,希望这些信息对大家有所帮助

java跳板执行ssh命令

maven依赖

<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>1.5.6</version>
</dependency>
<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-core</artifactId>
    <version>1.5.6</version>
</dependency>
<dependency>
    <groupId>com.jcraft</groupId>
    <artifactId>jsch</artifactId>
    <version>0.1.55</version>
</dependency>
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.lang.invoke.MethodHandles;

public class SSHJumpServerUtil {

    private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

    public static String executeCommandWithJumpServer(String jumpHost, String jumpUser, String jumpPassword, int jumpPort,
                                                    String targetHost, String targetUser, String targetPassword, int targetPort,
                                                    String command) {
        String excuteResult = null;
        try {
            JSch jsch = new JSch();

            // 创建跳板会话
            Session jumpSession = jsch.getSession(jumpUser, jumpHost, jumpPort);
            jumpSession.setPassword(jumpPassword);
            jumpSession.setConfig("StrictHostKeyChecking", "no");
            jumpSession.connect();

            // 创建跳板通道
            int forwardedPort = 10022; // 本地转发端口
            jumpSession.setPortForwardingL(forwardedPort, targetHost, targetPort);

            // 创建目标服务器会话
            Session targetSession = jsch.getSession(targetUser, "localhost", forwardedPort);
            targetSession.setPassword(targetPassword);
            targetSession.setConfig("StrictHostKeyChecking", "no");
            targetSession.connect();

            // 执行命令
            ChannelExec channelExec = (ChannelExec) targetSession.openChannel("exec");
            channelExec.setCommand(command);

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

            // 读取命令输出
            byte[] tmp = new byte[1024];
            while (true) {
                while (in.available() > 0) {
                    int i = in.read(tmp, 0, 1024);
                    if (i < 0) break;
                    excuteResult = new String(tmp, 0, i);
                }
                if (channelExec.isClosed()) {
                    if (in.available() > 0) continue;
                    LOGGER.debug("exit-status: " + channelExec.getExitStatus());
                    break;
                }
                try {
                    Thread.sleep(1000);
                } catch (Exception ee) {
                    ee.printStackTrace();
                }
            }

            // 关闭通道和会话
            channelExec.disconnect();
            targetSession.disconnect();
            jumpSession.disconnect();
        } catch (JSchException | IOException e) {
            LOGGER.error("跳板连接服务器执行异常",e);
        }

        return excuteResult;
    }
}

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文