我想使用JSch跳转到另一台主机,但它无法正常工作

时间:2016-11-13 05:19:28

标签: java jsch

我使用jsch连接到服务器,它使用Exec和shell通道发送命令,现在我想通过第一个连接连接到另一个服务器,但是当尝试连接第二个会话时应用程序崩溃,不确定如果我正确设置代码

public Session Connect() throws JSchException{

String host1 = "192.168.1.1";
String host2 = "192.168.2.2";
String username = "host";
String password = "pass";




JSch jsch = new JSch();
Session s;
s = jsch.getSession(username, host1, 22);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
s.setConfig(config);
s.setPassword(password);
s.connect();
s.setPortForwardingL(22, host2, 22);
Session secondSession = jsch.getSession(username, host2, 22);
config.put("StrictHostKeyChecking", "no");
secondSession.setConfig(config);
secondSession.setPassword(password);
secondSession.connect();

return secondSession;
 }

1 个答案:

答案 0 :(得分:1)

在您声明并分配给secondSession之后,您不是指setConfig上的setPasswordsecondSession而不是s上的secondSession.setConfig(config); secondSession.setPassword(password); secondSession.connect(); 吗? / p>

TypeStatusDetails