Java - Perform a FTP command through a SSH tunnel with JSch

。_饼干妹妹 提交于 2019-12-23 18:18:47

问题


I am developing a Java tool which uploads a file from one remote server to another.

The program will run on a laptop. The software needs to connect to serverA with SSH protocol then once it is connected to serverA, it has to transfer files to serverB through FTP. Files to be transfered are hosted on serverA.

I cannot directly connect to serverB because of a firewall.

Here is a summary:

Is it possible to do that with JSch? Something like the following:

JSch client = new JSch();
Session session = client.getSession("login", "serverA", 22);
// test purpose
session.setConfig("StrictHostKeyChecking", "no");
session.setPassword("password");
session.connect();
channel = (ChannelExec) session.openChannel("exec");
channel.setCommand("ftp -i ftp://username:password@serverB; put file.txt; close; quit;");

EDIT

What about writing a script and upload it on the serverA?

#!/bin/sh
ftp -n -i <<ENDOFINPUT
open serverB
user root password
cd /home/root
put xxx
close
bye
ENDOFINPUT

来源:https://stackoverflow.com/questions/12694499/java-perform-a-ftp-command-through-a-ssh-tunnel-with-jsch

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!