问题描述
我正在使用 java JSCH 库通过跳转服务器(server-1)连接到 server-2。使用下面的代码,我可以连接到跳转服务器(server-1)。使用相同的会话它无法连接到 server2 得到以下响应
JSch jsch = new JSch();
jsch.setKNownHosts("C:/My Program Files/eclipse/workspace/StatusTracker/kNown_hosts.txt");
// jsch.setKNownHosts(kNownHostLoc);
//Jump server connection session started
jumpServerSession = jsch.getSession(userid,jump server ip/hostname,22);
jumpServerSession.setPassword(jump server password);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking","no");
jumpServerSession.setConfig(config);
jumpServerSession.connect();
System.out.println("The session has been established to "+jump server userid+"@"+jump server name);
int assinged_port = jumpServerSession.setPortForwardingL(0,other server ip,22);
System.out.println("portforwarding: "+
"localhost:"+assinged_port+" -> "+other server ip+":"+22);
//Main server connection session started
targetServerSession = jsch.getSession(fileBO.getServerUserId(),"127.0.0.1",assinged_port);
targetServerSession.setHostKeyAlias(other server ip);
targetServerSession.setPassword(other server password);
java.util.Properties config1 = new java.util.Properties();
config1.put("StrictHostKeyChecking","no");
targetServerSession.setConfig(config1);
targetServerSession.connect();
channel = targetServerSession.openChannel("sftp");
channel = targetServerSession.openChannel("exec");
//command want to execute on dest server
((ChannelExec)channel).setCommand("pwd");
channel.setInputStream(null);
InputStream in11=channel.getInputStream();
((ChannelExec)channel).setErrStream(System.err);
channel.connect();
byte[] tmp1=new byte[1024];
while(true){
while(in11.available()>0){
int i1=in11.read(tmp1,1024);
if(i1<0)break;
System.out.print(new String(tmp1,i1));
}
if(channel.isClosed()){
System.out.println("exit-status: "+channel.getExitStatus());
break;
}
try{Thread.sleep(1000);}catch(Exception ee){}
}
} catch (final JSchException e) {
LOGGER.error(e.getMessage());
}
已连接 端口转发:本地主机:64038 -> jumpServerSession64038 com.jcraft.jsch.JSchException: Session.connect: java.net.socketException: 连接重置 在 com.jcraft.jsch.Session.connect(Session.java:565) 在 com.jcraft.jsch.Session.connect(Session.java:183) 在 com.test.appname.TestSCH.main(TestSCH.java:44)
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)