Search code examples
scalasftpjsch

Create a copy of the folder within same remote server with SFTP


I am using JSch API to connect to remote server through SFTP. I need to get a copy of a folder which is exist in the remote server into the same server. Is there any method implemented to do this kind of things in JSch? Or be kind to give me an advice to do the above use-case. (I am working in Scala).


Solution

  • We can not use "sftp" channel to do this task ans we have to use "exec" channel to do this task. Using "exec" channel we can execute Linux commands as follows,

    val command = "mkdir testDir"
    val channelExec: ChannelExec = session.openChannel("exec").asInstanceOf[ChannelExec]
    channelExec.setCommand(command);
    channelExec.connect
    

    Go through the following links to get more details

    http://www.programcreek.com/java-api-examples/index.php?api=com.jcraft.jsch.ChannelExec

    http://www.journaldev.com/246/java-program-to-run-shell-commands-on-ssh-enabled-system

    http://www.jcraft.com/jsch/examples/Exec.java.html

    Thank you for all participants