2
votes

I have a program that alternates between FTP and SFTP usage in order to perform some operations on a remote server.

For the FTP part, I am using the Apache Commons FTP client, whereas for the SFTP I am using the JSch library.

Apache commons, can and will, print out fully verbose all the commands issued by the client to the server, in addition with the server's response.

I would like to do something similar with JSch in order for me to be consistent in how my application logs in both cases.

I have tried implementing the Logger interface of the JSch library successfully, but it turns out that it cannot provide me with a verbose representation of the commands issued to the server, as well as it's response.

I have tried looking for similar solutions but they all revolve around doing something similar to I what I am looking for an SSH session and not for a SFTP session.

I have tried creating a PrintStream and then setting it as the OutputStream of the channel but this hasn't worked.

I have also tried implementing some suggestions I have found here that revolve around having a thread that will capture the channel's InputStream and OutputStreams but so far I have not been able to get something to work correctly.

My question is, has anyone managed to do something like that - e.g. printing out the underlying FTP dialog - while using the JSch library?

1

1 Answers

2
votes

The JSch library does not log SFTP requests and responses.

Consuming the streams won't help you either, because:

  • The ChannelSftp consumes the stream itself to read the packets. If you intervene, you will probably break its implementation.
  • The SFTP (contrary to the FTP) is a binary protocol. So even if you manage to read the stream, you get binary data, which you will need to decode. Essentially, it's quite difficult to implement logging of the SFTP protocol (comparing to the FTP), as you basically need to explicitly log every single field of every single type of request/response message.

Also note that the SFTP protocol is more low-level than the FTP. There are no "nice" high-level commands like FTP STOR or RETR. You may see thousands distinct requests for each transfer. Probably not, what you are after.