简体   繁体   中英

Using JSch to copy one file from remote machine to another remote machine

I have seen lot of examples of JSch where one can copy from local to remote machine and vice-versa. Is there a way in JSch where I can copy from one remote machine to another remote machine if I am able to provide login credentials for both remote machines.

I have tried following code

     JSch jsch=new JSch();
     Session session=jsch.getSession(user1, host1, 22);

     // username and password will be given via UserInfo interface.
     UserInfo ui=new MyUserInfo(password);
     session.setUserInfo(ui);
     session.connect();

     Session remoteSession = jsch.getSession(user2, host2, 22);
     UserInfo ui2 = new MyUserInfo(password2);
     remoteSession.setUserInfo(ui2);
     remoteSession.connect();

     // exec 'scp -f rfile' remotely
     String command="scp -f "+lfile;
     Channel channel=session.openChannel("exec");
     ((ChannelExec)channel).setCommand(command);

     Channel channel2=session.openChannel("exec");
     ((ChannelExec)channel2).setCommand(command);

     // get I/O streams for remote scp
     OutputStream out=channel.getOutputStream();
     InputStream in=channel2.getInputStream();

     channel.connect();
     channel2.connect();

which hangs after reading from input stream. Please let me know how could I achieve remote to remote copy.

关于我能想到实现这一目标的唯一方法是使用JSch发出原始SCP命令来执行从远程主机A到远程主机B的文件传输。为了做到这一点,你可能需要设置公共两个主机(A和B)之间的密钥验证,因此在连接到远程主机B时不需要以交互方式提供密码。虽然我相信可以通过连接通道提供此密码,但我只是不确定怎么样。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM