有一个用例,我必须:
第1点和第2点是直进的.
现在有一个外部服务器C (它提供了我的凭据)
/home/fixed/file.xlsx的服务器B中。如何实现这种多跳SFTP传输?
( B上不需要文件,一旦成功发送到C)
编辑:的回答帮助我做到了这一点。
@Override
public void createOurChannel(Path lastModifiedBankFile) {
LOG.info("Initiating SFTP for white-listed Server B for file: {}",
lastModifiedBankFile);
String host = properties.getServerBSftpHost();
String port = properties.getServerBSftpPort();
String password = properties.getServerBSftpPassword();
String username = properties.getServerBSftpUsername();
Session session = null;
try {
JSch ssh = new JSch();
JSch.setConfig("StrictHostKeyChecking", "no");
session = ssh.getSession(username, host, Integer.parseInt(port));
session.setPassword(password);
session.connect();
this.sendFileToBank(lastModifiedBankFile, session, ssh);
} catch (JSchException e) {
LOG.error("Jsch Exception occurred while SFTP.", e);
} finally {
if (session != null && session.isConnected())
session.disconnect();
LOG.info("Successfully disconnected from SFTP. {}");
}
}
@Override
public void sendFileToBank(Path lastModifiedBankFile, Session session, JSch ssh) {
Session sessionBank = null;
Channel channelBank = null;
ChannelSftp channelSftp = null;
String host = properties.getBankSftpHost();
String port = properties.getBankSftpPort();
String password = properties.getBankSftpPassword();
String username = properties.getBankSftpUsername();
String bankSftpDir = properties.getBankSftpEmiDir();
try {
int portForwarded = 2222;
session.setPortForwardingL(portForwarded, host, Integer.parseInt(port));
sessionBank = ssh.getSession(username, "localhost", portForwarded);
sessionBank.setPassword(password);
sessionBank.connect();
channelBank = sessionBank.openChannel("sftp");
channelBank.connect();
channelSftp = (ChannelSftp) channelBank;
channelSftp.put(lastModifiedBankFile.toAbsolutePath().toString(),
bankSftpDir + lastModifiedBankFile.getFileName().toString());
channelSftp.exit();
} catch (JSchException e) {
LOG.error("Jsch Exception occurred while SFTP.", e);
} catch (SftpException e) {
LOG.error("SFTP Exception occurred while SFTP.", e);
} finally {
if (channelBank != null && channelBank.isConnected())
channelBank.disconnect();
if (sessionBank != null && sessionBank.isConnected())
sessionBank.disconnect();
}
}发布于 2018-04-25 07:31:13
使用SSH隧道 (又名本地端口转发 )通过B打开到C的SSH/SFTP连接,然后可以从本地计算机(A)直接将文件上传到C,而无需先将文件上传到B:
Session sessionB = jsch.getSession("usernameB", "hostB", 22);
// ...
sessionB.connect();
int forwardedPort = 2222; // any port number which is not in use on the local machine
sessionB.setPortForwardingL(forwardedPort, "hostC", 22);
Session sessionC = jsch.getSession("usernameC", "localhost", forwardedPort);
// ...
sessionC.connect();
Channel channel = sessionC.openChannel("sftp");
channel.connect();
ChannelSftp channelSftp = (ChannelSftp)channel;
channelSftp.put("C:\\local\\path\\file.txt", "/remote/path/on/C/file.txt");必须注意:不要使用StrictHostKeyChecking=no__。这样做你就失去了安全感。请参阅https://stackoverflow.com/q/32852906/850848
https://stackoverflow.com/questions/50004632
复制相似问题