我使用java代码登录putty服务器后需要连接到另一台服务器,我需要在连接后从第二台服务器下载文件。有没有人有代码。
一旦出现putty服务器屏幕,我输入用户名和密码然后我传递命令“ssh servername”然后我需要输入yes以显示问题。
完成上述操作后,我必须导航到不同的文件夹并将文件下载到Server1。
我使用下面的代码连接到server1并连接,之后我不确定跳转到服务器2。
当我通过另一台服务器尝试远程服务器时,我收到如下错误。我在下面输入的错误和代码
错误:
Exception in thread "main" com.jcraft.jsch.JSchException: connection is closed by foreign host
at com.jcraft.jsch.Session.connect(Session.java:236)
at com.jcraft.jsch.Session.connect(Session.java:150)
at putty.putty.main(putty.java:42)
我使用的代码:
import java.io.IOException;
import java.io.InputStream;
import java.util.Vector;
import org.apache.commons.io.IOUtils;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;
public class putty {
public static String user = "username";
public static String host = "server1.server.net";
public static String password = "Password";
public static String secondpassword = "Password";
public static String tunnelRemoteHost = "server2.server.net";
public static void main(String[] args) throws IOException, JSchException, SftpException {
StringBuilder outputBuffer = new StringBuilder();
int port = 22;
JSch jsch = new JSch();
Session session = jsch.getSession(user, host, port);
session.setPassword(password);
session.setConfig("StrictHostKeyChecking", "no");
session.connect();
int forwardPort = 2222;
session.setPortForwardingL(forwardPort, tunnelRemoteHost, 22);
Session secondSession = jsch.getSession(user, "localhost", forwardPort);
secondSession.setPassword(secondpassword);
secondSession.setConfig("StrictHostKeyChecking", "no");
secondSession.connect();
session.openChannel("sftp");
// now we're connected to the secondary system
Channel channel = secondSession.openChannel("sftp");
channel.connect();
ChannelSftp channelSftp = (ChannelSftp)channel;
channel.disconnect();
session.disconnect();
System.out.print(outputBuffer.toString());
}
}
我正在使用最新的jsch jar 0.1.55。有人可以调查并告诉我这里我缺少的东西。
尝试将jsch更新为0.1.55 https://mvnrepository.com/artifact/com.jcraft/jsch/0.1.55
您当前的版本0.1.42已有近10年的历史。它可能不会改变任何东西或可能有帮助。
我能够通过Windows命令行使用plink命令实现我想要的功能