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;
    }
}
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值