好友
阅读权限10
听众
最后登录1970-1-1
|
25吾爱币
看看有没有高人指点:
问题场景:jar包的app部署在一台机器上,通过跳板机访问远程主机,并执行shell命令。下面是java代码:
问题涉及的核心代码如下:
========================================================
Session jumpSession = jsch.getSession(jumpUser, jumpHost, jumpPort);
jumpSession.setPassword(jumpPassword);
jumpSession.setConfig("StrictHostKeyChecking", "no");
jumpSession.connect();
int jumpPortForwarding = jumpSession.setPortForwardingL(0, remoteHost, remotePort);
Session session = jsch.getSession(remoteUser, "127.0.0.1", jumpPortForwarding);
session.setPassword(remotePassword);
session.setConfig("StrictHostKeyChecking", "no");
session.connect();
========================================================
最后一行报错,错误信息是:com.jcraft.jsch.JSchException: Session.connect: java.net.SocketException: Connection reset
jsch版本
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.55</version>
</dependency>
package com.ruoyi.common.utils.jsch;
import com.jcraft.jsch.*;
import com.ruoyi.common.enums.IPType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
/**
* 通过跳板机访问远程主机并非交互式执行单指令和多指令工具类
*/
@Slf4j
@Component
public class SSHTool {
@Value("${jumpServerUsername}")
private String jumpUser;
@Value("${jumpServerPassword}")
private String jumpPassword;
public void exec(String username, String password, String host, int port,List<String> cmds) throws JSchException, IOException {
IPEntity ipEntity = SSHTool.connectByJumper(host);
log.info("跳板机IP"+ipEntity.getTarget());
log.info("跳板机端口"+ipEntity.getPort());
log.info("跳板机用户名"+jumpUser);
log.info("跳板机密码"+jumpPassword);
log.info("远程主机IP"+host);
log.info("远程主机端口"+port);
log.info("远程主机密码"+password);
log.info("执行的指令:"+cmds);
executeCommands(ipEntity.getTarget(),ipEntity.getPort(),jumpUser,jumpPassword,host,port,username,password,cmds);
}
public void exec(String username, String password, String host, int port,String cmd) throws JSchException, IOException {
IPEntity ipEntity = SSHTool.connectByJumper(host);
executeCommand(ipEntity.getTarget(),ipEntity.getPort(),jumpUser,jumpPassword,host,port,username,password,cmd);
}
public static String executeCommands(String jumpHost, int jumpPort, String jumpUser, String jumpPassword,
String remoteHost, int remotePort, String remoteUser, String remotePassword,
List<String> commands) throws JSchException, IOException {
String result = "";
try {
JSch jsch = new JSch();
Session jumpSession = jsch.getSession(jumpUser, jumpHost, jumpPort);
jumpSession.setPassword(jumpPassword);
jumpSession.setConfig("StrictHostKeyChecking", "no");
jumpSession.connect();
int jumpPortForwarding = jumpSession.setPortForwardingL(0, remoteHost, remotePort);
Session session = jsch.getSession(remoteUser, "127.0.0.1", jumpPortForwarding);
session.setPassword(remotePassword);
session.setConfig("StrictHostKeyChecking", "no");
session.connect();
ChannelExec channel = (ChannelExec) session.openChannel("exec");
for (String command : commands) {
channel.setCommand(command);
InputStream in = channel.getInputStream();
channel.connect();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line;
while ((line = reader.readLine()) != null) {
result += line + "\n";
}
channel.disconnect();
}
session.disconnect();
jumpSession.disconnect();
} catch (JSchException | IOException e) {
log.info("ssh异常:"+e.getMessage());
e.printStackTrace();
throw new RuntimeException(e.getMessage());
}
return result;
}
public static String executeCommand(String jumpHost, int jumpPort, String jumpUser, String jumpPassword,
String remoteHost, int remotePort, String remoteUser, String remotePassword,
String command) throws JSchException, IOException {
List<String> commands = new ArrayList<>();
commands.add(command);
return executeCommands(jumpHost, jumpPort, jumpUser, jumpPassword, remoteHost, remotePort, remoteUser, remotePassword, commands);
}
public static IPEntity connectByJumper(String source){
String off = source.substring(0,6);
String target = "";
Integer targetPort = 0;
if (off.equals(IPType.BJ.getSource())){
target = IPType.BJ.getTarget();
targetPort = IPType.BJ.getPort();
} else if (off.equals(IPType.GZ.getSource())) {
target = IPType.GZ.getTarget();
targetPort = IPType.GZ.getPort();
} else if (off.equals(IPType.NM_A_1.getSource())){
target = IPType.NM_A_1.getTarget();
targetPort = IPType.NM_A_1.getPort();
} else if (off.equals(IPType.NM_A_2.getTarget())){
target = IPType.NM_A_2.getTarget();
targetPort = IPType.NM_A_2.getPort();
} else if (off.equals(IPType.NM_B.getSource())) {
target = IPType.NM_B.getTarget();
targetPort = IPType.NM_B.getPort();
} else if (off.equals(IPType.SH_PROD.getSource())) {
target = IPType.SH_PROD.getTarget();
targetPort = IPType.SH_PROD.getPort();
} else if (off.equals(IPType.SH_TEST.getSource())) {
target = IPType.SH_TEST.getTarget();
targetPort = IPType.SH_TEST.getPort();
}
IPEntity ipEntity = new IPEntity();
ipEntity.setTarget(target);
ipEntity.setPort(targetPort);
return ipEntity;
}
public static void main(String[] args) throws JSchException, IOException {
String jumpServerHost = "jump.server.com";
int jumpServerPort = 22;
String jumpServerUsername = "jump_user";
String jumpServerPassword = "jump_password";
String remoteHost = "remote.server.com";
int remotePort = 22;
String remoteUsername = "remote_user";
String remotePassword = "remote_password";
List<String> commands = new ArrayList<>();
commands.add("ls");
commands.add("pwd");
String result1 = executeCommands(jumpServerHost, jumpServerPort, jumpServerUsername, jumpServerPassword, remoteHost, remotePort, remoteUsername, remotePassword, commands);
System.out.println(result1);
String command = "ls -l";
String result2 = executeCommand(jumpServerHost, jumpServerPort, jumpServerUsername, jumpServerPassword, remoteHost, remotePort, remoteUsername, remotePassword, command);
System.out.println(result2);
}
}
|
|