Step 1: Session connect
....
session.connect()
Step 2: Shell channel
channel = session.openChannel("shell");
Step 3: Execute ssh commands to login Unix server and go to the required path cd /logs/server
PrintStream shellStream = new PrintStream(channel.getOutputStream()); // printStream for convenience
channel.connect();
shellStream.println(command);
shellStream.flush();
step 4: Get the file from Unix server and put into SFTP:
code to connect sftp channel and put the file from unix server to sftp.
So I want to upload the file (logs/server/server.log
) from Unix server (which I logged in using shell channel) to SFTP server.
Both source and destination are remote.
Is it possible to do by using JSch?
An SFTP protocol (let alone JSch SFTP library) has no support for transfers between two remote servers.
If you have a shell access to one of the servers, you can try to transfer the files by using a command-line (OpenSSH) sftp
client on the server. It's doable. Problematic is an authentication part, as it's non-interactive session. You would have to use an unencrypted private key, an agent forwarding or sshpass
.
Obvious and simple implementation is to download the files from the "Unix" server to a local machine and then upload them to the "SFTP" server. But you need to use the SFTP channel, not the shell channel, for the download.