You are not logged in.

Dear visitor, welcome to Jabaco - Community. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

jbExplorer

Trainee

  • "jbExplorer" started this thread

Posts: 111

Date of registration: Mar 18th 2013

  • Send private message

1

Friday, March 14th 2014, 1:22pm

Using JSCH to get a list of files from an sftp server

Import com#jcraft#jsch#Channel
Import com#jcraft#jsch#ChannelSftp
Import com#jcraft#jsch#Jsch
Import com#jcraft#jsch#JSchException
Import com#jcraft#jsch#Session
Import com#jcraft#jsch#SftpException
Import java#util#Vector



Public Sub main(ByJava args() As String)
Dim myArgs() As String
myArgs = args

Dim sResult As String = "" '// Generic return value
Dim sHostName As String
Dim sUserName As String
Dim sPassword As String
Dim sDirectory As String

Dim prpConfig As New Properties()
Dim sshDemo As JSch
Dim sesSSH As Session
Dim chnSFTP As Channel
Dim chnsftpFiles As ChannelSftp
Dim vFiles As Vector
Dim nNumberOfFiles As Integer
Dim nthFile As Integer
Dim sNthFile As String

On Error Goto ErrHandler

prpConfig = New Properties()
System.out.println( "Setting strict host key checking off" )
prpConfig.put( "StrictHostKeyChecking", "no" )

sHostName = "files.mysite.com"

sUserName = "myusername"
sPassword = "mypassword"
sDirectory = "/dis_folder/and/dat_folder/and/deoder_folder_dere"

System.out.println( "Creating JSCH instance" )
sshDemo = New JSch()

System.out.println( "Getting a session " & "Username: " & sUserName & ", " & " Hostname: " & sHostName )
sesSSH = sshDemo.getSession( sUserName, sHostName, 22 )

System.out.println( "Setting the config property for the session" )
sesSSH.setConfig( prpConfig )

sesSSH.setPassword( sPassword )

System.out.println( "Connecting the ssh session" )
sesSSH.connect()

System.out.println( "Opening an sftp channel" )
chnSFTP = sesSSH.openChannel( "sftp" )

System.out.println( "Connecting the channel" )
chnSFTP.connect()

chnsftpFiles = Cast( chnSFTP, ChannelSftp )
chnsftpFiles.cd( sDirectory )
vFiles = chnsftpFiles.ls( "*" )
nNumberOfFiles = vFiles.size

System.out.println( "Directory " & sDirectory & " " & "has " & nNumberOfFiles )
For nthFile = 1 To nNumberOfFiles
sNthFile = vFiles.get( nthFile ).toString
System.out.println( "Filename: & " & sNthFile )
Debug().Print( "Filename: & " & sNthFile )
Next 'nthFile = 1 To nNumberOfFiles

chnsftpFiles.disconnect
sesSSH.disconnect

'// Temporary breakpoint. Remove, after development.
sResult = sResult

Exit Sub

ErrHandler:
JabacoErrorManager( "JSCH_Example : " & Err.getClass.getName, "what the...? ", Err.getMessage )


End Sub

Rate this thread
WoltLab Burning Board