Skip to content
Advertisement

How to execute shell command using SFTP channel in JSch?

I am trying to list all *.xml files in the directory. I do a cd first and then was trying to execute:

JavaScript

But not sure how exactly to do it. There were some example around the Exec channel but is there a way to do the find with SFTP itself?

JavaScript

Advertisement

Answer

Your requirements are conflicting.

You cannot execute shell commands with SFTP.
(That’s true in any case, no matter what language or library are you using).


So you have to choose.

  • Either run your find shell command with “Exec channel”.
  • Or use a pure SFTP interface via ChannelSftp.ls (and programatically recurse into subdirectories). This approach is a way more laborious and will be magnitudes slower. On the other hand it will be a way more robust. And it is actually the only solution, if you do not have a shell access in the first place. Not to mention that it is platform-independent (what find command is not).
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement