Skip to content

Instantly share code, notes, and snippets.

@kiy0taka
Created December 29, 2009 12:51
Show Gist options
  • Save kiy0taka/265295 to your computer and use it in GitHub Desktop.
Save kiy0taka/265295 to your computer and use it in GitHub Desktop.
Groovy/Commons VFS/SFTP
@Grab('commons-vfs:commons-vfs:1.0')
@Grab('com.jcraft:jsch:0.1.42')
import com.jcraft.jsch.*
import org.apache.commons.vfs.*
import org.apache.commons.vfs.provider.sftp.*
def url = 'sftp://username@host[:port]/path'
def keyFilePath = 'path/to/keyfile'
def passphrase = 'passphrase'
def fileSystemManager = VFS.manager
if (!fileSystemManager.hasProvider('sftp')) {
fileSystemManager.addProvider('sftp', new SftpFileProvider())
}
def opts = new FileSystemOptions()
SftpFileSystemConfigBuilder.getInstance().with {
it.setStrictHostKeyChecking(opts, "no");
it.setIdentities(opts, [new File(keyFilePath)] as File[])
it.setUserInfo(opts, [getPassphrase:{passphrase}, promptPassphrase:{true}] as UserInfo)
}
fileSystemManager.resolveFile(url, opts).children.each {
println it.name
}
@madmas
Copy link

madmas commented Nov 25, 2015

Have a look at https://github.com/ysb33r/groovy-vfs, it makes such things with groovy a lot simpler :)

@chhex
Copy link

chhex commented Sep 1, 2017

madmas , not true, groovy-vfs has 30 issues , none resolved , not very active, examples fail , typical example of developer wanting to much , achieving less .... here i have a example that works

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment