views:

153

answers:

3

VB2005 and SharpSSH. Using the library to connect to a remote server and copy a file. I have the process working fairly well but have some smaller things which I cant seem to resolve as documentation for the library is fairly thin.

I have two routines working. One using the Tamir.SharpSsh class and the other using the Tamir.SharpSsh.jsch class.

  1. Using the Tamir.SharpSsh class I am able to copy the file from the local server to the remote server and tap into the pogress event. What I can't do is determine if a particular file on the remote server say /Report/data.txt exists on the server. I need to take different actions if it exists or if doesn't exist. Also how would I rename a file on the remote server. Ive tried using SshExec with a 'rename', 'rn', and 'mv' command and it doesn't seem to work.

  2. Using the Tamir.SharpSsh.jsch I can copy the file from the local server to the remote server. I can also rename the file on the remote server. What I cant do with this class is to tap into the progress event to keep track of the copy progress. Also I cant seem to find a good way to test to see if a particular file exists on the server. What I have come up with is crude and the only way that I could come up with to test and that is to use

        Dim c As ChannelSftp
        Dim vct As Tamir.SharpSsh.java.util.Vector = c.ls(sRemoteFile)
        Dim cnt As Integer = vct.Count
    

When one or more file exists I get a count no problem. When there is no file then an exception is thrown.

Anyway, I have the routines working its just some minor things I need help with.

tia AGP

A: 

Your issues are because of limitations of SFTP protocol. - to check file existence, try to return attributes of that file; - most servers don't support file renaming for now.

Nickolay O.
Im not certain I understand. I can rename the file using Tamir.SharpSsh.jsch no problem. I just want to do the same with Tamir.SharpSsh. i can also rename using the GUI of Bitvise Tunnelier. Ive done it a hundred times, now I just want to automate it. I'll check on file attributes but could have sworn I already tried that.
sinDizzy
Nickolay, SFTP does support renaming of files. It also supports a lot more. It's the _implementation_ (SharpSsh) that does not support them. I am refering to the SSH_FXP_RENAME packet type listed on page 7 of the protocol spec: http://tools.ietf.org/html/draft-ietf-secsh-filexfer-13
rmx
Yes, protocol supports this feature. But most SFTP protocol implementations are not.
Nickolay O.
A: 

You can call the Tamir.SharpSsh.Sftp.GetFile method using the path of the file you want to check exists (example in C#, sorry):

private bool FileExists(string filePath)
{
    try
    {
        SftpConnection connection = new SftpConnection(_host, _username, _password);
        connection.Connect(_port);
        connection.Get(filePath, _toDir);
    }
    catch(JSchException)
    {
        return false;
    }
    return true;
}

I have also noticed a few other issues through my use of this library - like a lack of a GetFileInfo method or recursive Gets and Puts. But overall it get the job done.

The simple fact is, Tamir.SharpSsh can't rename a file remotely - it just does not implement that functionality. You can purchase a better library that has far more features, such as:

  • Kellerman Software .NET SFTP Library
  • wodSFTP.NET
  • Rebex SFTP for .NET
  • edtFTPnet/PRO

or you could extend SharpSsh, since it is open source.

rmx
yeah I tried something similar with Tamir.SharpSsh.jsch but it seems odd to me that you have to cathc the exception to dwtect non-existance of a file. here is what i did a while back
sinDizzy
Yes, it is odd. And furthermore, its bad practice to use exceptions for logic rather than for error handling. The only other suggestion I can make is to try the Rebex product (http://www.rebex.net/sftp.net/) which does have a `FileExists` method to check if a file exists on the server. The APIs are largely the same so it would be easy to migrate to but it is not free.
rmx
A: 

yeah I tried something similar with Tamir.SharpSsh.jsch but it seems odd to me that you have to catch the exception to detect non-existence of a file. here is what i did after I posted:

Private Function FileExistsOnServer(ByVal c As ChannelSftp, ByVal sRemoteFile As String) As Boolean
    Try
        'get a file listing of the file
        Dim vct As Tamir.SharpSsh.java.util.Vector = c.ls(sRemoteFile)
        Dim cnt As Integer = vct.Count

        'if the count is greater than zero then the file already exists. if its 0 then the file does
        'not exist on the server
        If cnt > 0 Then
            Return True
        Else
            Return False
        End If
    Catch ex As Exception
        'if we get an exception then assume the file does not exist on the server
        Return False
    End Try
End Function
sinDizzy