尝试使用JSch将文件传输到远程主机,但是必须通过跳转主机将其连接。我可以连接到跳转主机,但是在尝试连接到远程主机时出现异常。
我尝试通过在工作站上使用SSH命令进行验证,并且能够连接。在Java中,我尝试遵循以下步骤: JSch multiple tunnels/jumphosts
但是,我收到“线程“ main” com.jcraft.jsch.JSchException中的异常:java.net.ConnectException:连接被拒绝:connect”。下面是我的代码:
String jumpHost = "sshgateway";
String remoteHost = "cimtst1a";
String jumpHostKnownHostsPath = "D:\\OpenSSH-Win64\\sshgateway_known_hosts";
String remoteHostKnownHostsPath = "D:\\OpenSSH-Win64\\cimtst1a_known_hosts";
String jumpHostUsername = "wsh";
String jumpHostPassword = "adsf";
String remoteHostUsername = "cim";
String remoteHostPassword = "adsf";
JSch jsch = new JSch();
jsch.setKnownHosts(jumpHostKnownHostsPath);
Session jumpHostSession = jsch.getSession( jumpHostUsername, jumpHost );
jumpHostSession.setConfig(
"PreferredAuthentications",
"publickey,keyboard-interactive,password"
);
jumpHostSession.setPassword( jumpHostPassword );
jumpHostSession.connect();
if(jumpHostSession.isConnected())
{
logger.info("Jumphost Session created to " + jumpHostSession.getHost() + " by " + jumpHostSession.getUserName());
}
int remoteHostSshPort = jumpHostSession.setPortForwardingL(0, remoteHost, 22);
jsch.setKnownHosts(remoteHostKnownHostsPath);
Session remoteHostSession = jsch.getSession( remoteHostUsername, remoteHost, remoteHostSshPort);
remoteHostSession.setConfig(
"PreferredAuthentications",
"publickey,keyboard-interactive,password"
);
remoteHostSession.setPassword( remoteHostPassword );
remoteHostSession.connect();
if(!remoteHostSession.isConnected())
{
System.out.println("connected~!");
}